Skip to content

fix: skip validation if tool reports error #655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
66 changes: 66 additions & 0 deletions src/server/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,72 @@ describe("tool()", () => {
).rejects.toThrow(/Tool test has an output schema but no structured content was provided/);
});

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

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

// 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}`,
},
],
isError: true,
})
);

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

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

// Call the tool and expect it to not throw an error
await expect(
client.callTool({
name: "test",
arguments: {
input: "hello",
},
}),
).resolves.toStrictEqual({
content: [
{
type: "text",
text: `Processed: hello`,
},
],
isError: true,
});
});

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

if (tool.outputSchema) {
if (tool.outputSchema && (result.isError !== true)) {
if (!result.structuredContent) {
throw new McpError(
ErrorCode.InvalidParams,
Expand Down