Skip to content

Commit 7636c63

Browse files
committed
update to track spec changes
1 parent 265ad07 commit 7636c63

File tree

6 files changed

+47
-281
lines changed

6 files changed

+47
-281
lines changed

src/examples/client/testOutputSchemaServers.ts

Lines changed: 0 additions & 89 deletions
This file was deleted.

src/examples/server/lowLevelOutputSchema.ts

Lines changed: 0 additions & 116 deletions
This file was deleted.

src/examples/server/mcpServerOutputSchema.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,26 @@ server.registerTool(
4545
const temp_c = Math.round((Math.random() * 35 - 5) * 10) / 10;
4646
const conditions = ["sunny", "cloudy", "rainy", "stormy", "snowy"][Math.floor(Math.random() * 5)];
4747

48-
return {
49-
content: [],
50-
structuredContent: {
51-
temperature: {
52-
celsius: temp_c,
53-
fahrenheit: Math.round((temp_c * 9 / 5 + 32) * 10) / 10
54-
},
55-
conditions,
56-
humidity: Math.round(Math.random() * 100),
57-
wind: {
58-
speed_kmh: Math.round(Math.random() * 50),
59-
direction: ["N", "NE", "E", "SE", "S", "SW", "W", "NW"][Math.floor(Math.random() * 8)]
60-
}
48+
const structuredContent = {
49+
temperature: {
50+
celsius: temp_c,
51+
fahrenheit: Math.round((temp_c * 9 / 5 + 32) * 10) / 10
52+
},
53+
conditions,
54+
humidity: Math.round(Math.random() * 100),
55+
wind: {
56+
speed_kmh: Math.round(Math.random() * 50),
57+
direction: ["N", "NE", "E", "SE", "S", "SW", "W", "NW"][Math.floor(Math.random() * 8)]
6158
}
6259
};
60+
61+
return {
62+
content: [{
63+
type: "text",
64+
text: JSON.stringify(structuredContent, null, 2)
65+
}],
66+
structuredContent
67+
};
6368
}
6469
);
6570

src/server/mcp.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,18 +1224,15 @@ describe("tool()", () => {
12241224
mcpServer.server.connect(serverTransport),
12251225
]);
12261226

1227-
// First call listTools to cache the outputSchema in the client
1228-
await client.listTools();
1229-
1230-
// Call the tool and expect it to throw a validation error
1227+
// Call the tool and expect it to throw a server-side validation error
12311228
await expect(
12321229
client.callTool({
12331230
name: "test",
12341231
arguments: {
12351232
input: "hello",
12361233
},
12371234
}),
1238-
).rejects.toThrow(/Structured content does not match the tool's output schema/);
1235+
).rejects.toThrow(/Invalid structured content for tool test/);
12391236
});
12401237

12411238
/***

src/server/mcp.ts

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -198,40 +198,15 @@ export class McpServer {
198198
}
199199
}
200200

201-
// Handle structured output and backward compatibility
202-
if (tool.outputSchema) {
203-
// Tool has outputSchema, so result must have structuredContent (unless it's an error)
204-
if (!result.structuredContent && !result.isError) {
205-
throw new McpError(
206-
ErrorCode.InternalError,
207-
`Tool ${request.params.name} has outputSchema but returned no structuredContent`,
208-
);
209-
}
210-
211-
// For backward compatibility, if structuredContent is provided but no content,
212-
// automatically serialize the structured content to text
213-
if (result.structuredContent && !result.content) {
214-
result.content = [
215-
{
216-
type: "text",
217-
text: JSON.stringify(result.structuredContent, null, 2),
218-
},
219-
];
220-
}
221-
} else {
222-
// Tool must have content if no outputSchema
223-
if (!result.content && !result.isError) {
224-
throw new McpError(
225-
ErrorCode.InternalError,
226-
`Tool ${request.params.name} has no outputSchema and must return content`,
227-
);
228-
}
229-
230-
// If structuredContent is provided, it's an error
231-
if (result.structuredContent) {
201+
if (tool.outputSchema && result.structuredContent) {
202+
// if the tool has an output schema, validate structured content
203+
const parseResult = await tool.outputSchema.safeParseAsync(
204+
result.structuredContent,
205+
);
206+
if (!parseResult.success) {
232207
throw new McpError(
233-
ErrorCode.InternalError,
234-
`Tool ${request.params.name} has no outputSchema but returned structuredContent`,
208+
ErrorCode.InvalidParams,
209+
`Invalid structured content for tool ${request.params.name}: ${parseResult.error.message}`,
235210
);
236211
}
237212
}

0 commit comments

Comments
 (0)