Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/server/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,70 @@ describe("tool()", () => {
expect(JSON.parse(textContent.text)).toEqual(result.structuredContent);
});

/***
* Test: Tool with Output Schema Must Provide Structured Content
*/
test("should throw error when tool with outputSchema returns no structuredContent", async () => {
const mcpServer = new McpServer({
name: "test server",
version: "1.0",
});

const client = new Client(
{
name: "test client",
version: "1.0",
},
{
capabilities: {
tools: {},
},
},
);

// Register a tool with outputSchema that returns only content without structuredContent
mcpServer.registerTool(
"test",
{
description: "Test tool with output schema but missing structured content",
inputSchema: {
input: z.string(),
},
outputSchema: {
processedInput: z.string(),
resultType: z.string(),
},
},
async ({ input }) => ({
// Only return content without structuredContent
content: [
{
type: "text",
text: `Processed: ${input}`,
},
],
})
);

const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();

await Promise.all([
client.connect(clientTransport),
mcpServer.server.connect(serverTransport),
]);

// Call the tool and expect it to throw an error
await expect(
client.callTool({
name: "test",
arguments: {
input: "hello",
},
}),
).rejects.toThrow(/Tool test has an output schema but no structured content was provided/);
});

/***
* Test: Schema Validation Failure for Invalid Structured Content
*/
Expand Down
9 changes: 8 additions & 1 deletion src/server/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,14 @@ export class McpServer {
}
}

if (tool.outputSchema && result.structuredContent) {
if (tool.outputSchema) {
if (!result.structuredContent) {
throw new McpError(
ErrorCode.InvalidParams,
`Tool ${request.params.name} has an output schema but no structured content was provided`,
);
}

// if the tool has an output schema, validate structured content
const parseResult = await tool.outputSchema.safeParseAsync(
result.structuredContent,
Expand Down