Skip to content

Commit e76faf6

Browse files
author
joeyzzeng
committed
fix: skip validation if tool reports error
1 parent 9b99ffb commit e76faf6

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/server/mcp.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,72 @@ describe("tool()", () => {
12041204
).rejects.toThrow(/Tool test has an output schema but no structured content was provided/);
12051205
});
12061206

1207+
/***
1208+
* Test: Tool with Output Schema Must Provide Structured Content
1209+
*/
1210+
test("should not throw error when tool with outputSchema returns no structuredContent and isError is true", async () => {
1211+
const mcpServer = new McpServer({
1212+
name: "test server",
1213+
version: "1.0",
1214+
});
1215+
1216+
const client = new Client({
1217+
name: "test client",
1218+
version: "1.0",
1219+
});
1220+
1221+
// Register a tool with outputSchema that returns only content without structuredContent
1222+
mcpServer.registerTool(
1223+
"test",
1224+
{
1225+
description: "Test tool with output schema but missing structured content",
1226+
inputSchema: {
1227+
input: z.string(),
1228+
},
1229+
outputSchema: {
1230+
processedInput: z.string(),
1231+
resultType: z.string(),
1232+
},
1233+
},
1234+
async ({ input }) => ({
1235+
// Only return content without structuredContent
1236+
content: [
1237+
{
1238+
type: "text",
1239+
text: `Processed: ${input}`,
1240+
},
1241+
],
1242+
isError: true,
1243+
})
1244+
);
1245+
1246+
const [clientTransport, serverTransport] =
1247+
InMemoryTransport.createLinkedPair();
1248+
1249+
await Promise.all([
1250+
client.connect(clientTransport),
1251+
mcpServer.server.connect(serverTransport),
1252+
]);
1253+
1254+
// Call the tool and expect it to not throw an error
1255+
await expect(
1256+
client.callTool({
1257+
name: "test",
1258+
arguments: {
1259+
input: "hello",
1260+
},
1261+
}),
1262+
).resolves.toStrictEqual({
1263+
content: [
1264+
{
1265+
type: "text",
1266+
text: `Processed: hello`,
1267+
},
1268+
],
1269+
isError: true,
1270+
});
1271+
});
1272+
12071273
/***
12081274
* Test: Schema Validation Failure for Invalid Structured Content
12091275
*/

src/server/mcp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export class McpServer {
200200
}
201201
}
202202

203-
if (tool.outputSchema) {
203+
if (tool.outputSchema && (result.isError !== true)) {
204204
if (!result.structuredContent) {
205205
throw new McpError(
206206
ErrorCode.InvalidParams,

0 commit comments

Comments
 (0)