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
10 changes: 1 addition & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

142 changes: 92 additions & 50 deletions src/server/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -928,37 +928,53 @@ describe('tool()', () => {

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

await expect(
client.request(
{
method: 'tools/call',
params: {
const result = await client.request(
{
method: 'tools/call',
params: {
name: 'test',
arguments: {
name: 'test',
arguments: {
name: 'test',
value: 'not a number'
}
value: 'not a number'
}
},
CallToolResultSchema
)
).rejects.toThrow(/Invalid arguments/);
}
},
CallToolResultSchema
);

await expect(
client.request(
expect(result.isError).toBe(true);
expect(result.content).toEqual(
expect.arrayContaining([
{
method: 'tools/call',
params: {
name: 'test (new api)',
arguments: {
name: 'test',
value: 'not a number'
}
type: 'text',
text: expect.stringContaining('Input validation error: Invalid arguments for tool test')
}
])
);

const result2 = await client.request(
{
method: 'tools/call',
params: {
name: 'test (new api)',
arguments: {
name: 'test',
value: 'not a number'
}
},
CallToolResultSchema
)
).rejects.toThrow(/Invalid arguments/);
}
},
CallToolResultSchema
);

expect(result2.isError).toBe(true);
expect(result2.content).toEqual(
expect.arrayContaining([
{
type: 'text',
text: expect.stringContaining('Input validation error: Invalid arguments for tool test (new api)')
}
])
);
});

/***
Expand Down Expand Up @@ -1152,14 +1168,24 @@ describe('tool()', () => {
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'
const result = await client.callTool({
name: 'test',
arguments: {
input: 'hello'
}
});

expect(result.isError).toBe(true);
expect(result.content).toEqual(
expect.arrayContaining([
{
type: 'text',
text: expect.stringContaining(
'Output validation error: Tool test has an output schema but no structured content was provided'
)
}
})
).rejects.toThrow(/Tool test has an output schema but no structured content was provided/);
])
);
});
/***
* Test: Tool with Output Schema Must Provide Structured Content
Expand Down Expand Up @@ -1274,14 +1300,22 @@ describe('tool()', () => {
await Promise.all([client.connect(clientTransport), mcpServer.server.connect(serverTransport)]);

// Call the tool and expect it to throw a server-side validation error
await expect(
client.callTool({
name: 'test',
arguments: {
input: 'hello'
const result = await client.callTool({
name: 'test',
arguments: {
input: 'hello'
}
});

expect(result.isError).toBe(true);
expect(result.content).toEqual(
expect.arrayContaining([
{
type: 'text',
text: expect.stringContaining('Output validation error: Invalid structured content for tool test')
}
})
).rejects.toThrow(/Invalid structured content for tool test/);
])
);
});

/***
Expand Down Expand Up @@ -1552,17 +1586,25 @@ describe('tool()', () => {

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

await expect(
client.request(
const result = await client.request(
{
method: 'tools/call',
params: {
name: 'nonexistent-tool'
}
},
CallToolResultSchema
);

expect(result.isError).toBe(true);
expect(result.content).toEqual(
expect.arrayContaining([
{
method: 'tools/call',
params: {
name: 'nonexistent-tool'
}
},
CallToolResultSchema
)
).rejects.toThrow(/Tool nonexistent-tool not found/);
type: 'text',
text: expect.stringContaining('Tool nonexistent-tool not found')
}
])
);
});

/***
Expand Down
114 changes: 57 additions & 57 deletions src/server/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,73 +126,55 @@ export class McpServer {

this.server.setRequestHandler(CallToolRequestSchema, async (request, extra): Promise<CallToolResult> => {
const tool = this._registeredTools[request.params.name];
if (!tool) {
throw new McpError(ErrorCode.InvalidParams, `Tool ${request.params.name} not found`);
}

if (!tool.enabled) {
throw new McpError(ErrorCode.InvalidParams, `Tool ${request.params.name} disabled`);
}

let result: CallToolResult;

if (tool.inputSchema) {
const parseResult = await tool.inputSchema.safeParseAsync(request.params.arguments);
if (!parseResult.success) {
throw new McpError(
ErrorCode.InvalidParams,
`Invalid arguments for tool ${request.params.name}: ${parseResult.error.message}`
);
try {
if (!tool) {
throw new McpError(ErrorCode.InvalidParams, `Tool ${request.params.name} not found`);
}

const args = parseResult.data;
const cb = tool.callback as ToolCallback<ZodRawShape>;
try {
result = await Promise.resolve(cb(args, extra));
} catch (error) {
result = {
content: [
{
type: 'text',
text: error instanceof Error ? error.message : String(error)
}
],
isError: true
};
}
} else {
const cb = tool.callback as ToolCallback<undefined>;
try {
result = await Promise.resolve(cb(extra));
} catch (error) {
result = {
content: [
{
type: 'text',
text: error instanceof Error ? error.message : String(error)
}
],
isError: true
};
if (!tool.enabled) {
throw new McpError(ErrorCode.InvalidParams, `Tool ${request.params.name} disabled`);
}
}

if (tool.outputSchema && !result.isError) {
if (!result.structuredContent) {
throw new McpError(
ErrorCode.InvalidParams,
`Tool ${request.params.name} has an output schema but no structured content was provided`
);
if (tool.inputSchema) {
const cb = tool.callback as ToolCallback<ZodRawShape>;
const parseResult = await tool.inputSchema.safeParseAsync(request.params.arguments);
if (!parseResult.success) {
throw new McpError(
ErrorCode.InvalidParams,
`Input validation error: Invalid arguments for tool ${request.params.name}: ${parseResult.error.message}`
);
}

const args = parseResult.data;

result = await Promise.resolve(cb(args, extra));
} else {
const cb = tool.callback as ToolCallback<undefined>;
result = await Promise.resolve(cb(extra));
}

// if the tool has an output schema, validate structured content
const parseResult = await tool.outputSchema.safeParseAsync(result.structuredContent);
if (!parseResult.success) {
throw new McpError(
ErrorCode.InvalidParams,
`Invalid structured content for tool ${request.params.name}: ${parseResult.error.message}`
);
if (tool.outputSchema && !result.isError) {
if (!result.structuredContent) {
throw new McpError(
ErrorCode.InvalidParams,
`Output validation error: 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);
if (!parseResult.success) {
throw new McpError(
ErrorCode.InvalidParams,
`Output validation error: Invalid structured content for tool ${request.params.name}: ${parseResult.error.message}`
);
}
}
} catch (error) {
return this.createToolError(error instanceof Error ? error.message : String(error));
}

return result;
Expand All @@ -201,6 +183,24 @@ export class McpServer {
this._toolHandlersInitialized = true;
}

/**
* Creates a tool error result.
*
* @param errorMessage - The error message.
* @returns The tool error result.
*/
private createToolError(errorMessage: string): CallToolResult {
return {
content: [
{
type: 'text',
text: errorMessage
}
],
isError: true
};
}

private _completionHandlerInitialized = false;

private setCompletionRequestHandler() {
Expand Down