fix: correct error handling for unknown tools and resources (#1389)
Co-authored-by: Felix Weinberger <3823880+felixweinberger@users.noreply.github.com>
This commit is contained in:
parent
ad4b2f9cac
commit
108f2f3ab6
1 changed files with 123 additions and 23 deletions
|
|
@ -1777,22 +1777,59 @@ describe('Zod v4', () => {
|
|||
|
||||
await Promise.all([client.connect(clientTransport), mcpServer.server.connect(serverTransport)]);
|
||||
|
||||
const result = await client.request({
|
||||
method: 'tools/call',
|
||||
params: {
|
||||
name: 'nonexistent-tool'
|
||||
}
|
||||
await expect(
|
||||
client.request({
|
||||
method: 'tools/call',
|
||||
params: {
|
||||
name: 'nonexistent-tool'
|
||||
}
|
||||
})
|
||||
).rejects.toMatchObject({
|
||||
code: ProtocolErrorCode.InvalidParams,
|
||||
message: expect.stringContaining('nonexistent-tool')
|
||||
});
|
||||
});
|
||||
|
||||
/***
|
||||
* Test: ProtocolError for Disabled Tool
|
||||
*/
|
||||
test('should throw ProtocolError for disabled tool', async () => {
|
||||
const mcpServer = new McpServer({
|
||||
name: 'test server',
|
||||
version: '1.0'
|
||||
});
|
||||
|
||||
expect(result.isError).toBe(true);
|
||||
expect(result.content).toEqual(
|
||||
expect.arrayContaining([
|
||||
const client = new Client({
|
||||
name: 'test client',
|
||||
version: '1.0'
|
||||
});
|
||||
|
||||
const tool = mcpServer.registerTool('test-tool', {}, async () => ({
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: expect.stringContaining('Tool nonexistent-tool not found')
|
||||
text: 'Test response'
|
||||
}
|
||||
])
|
||||
);
|
||||
]
|
||||
}));
|
||||
|
||||
tool.disable();
|
||||
|
||||
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
||||
|
||||
await Promise.all([client.connect(clientTransport), mcpServer.server.connect(serverTransport)]);
|
||||
|
||||
await expect(
|
||||
client.request({
|
||||
method: 'tools/call',
|
||||
params: {
|
||||
name: 'test-tool'
|
||||
}
|
||||
})
|
||||
).rejects.toMatchObject({
|
||||
code: ProtocolErrorCode.InvalidParams,
|
||||
message: expect.stringContaining('disabled')
|
||||
});
|
||||
});
|
||||
|
||||
/***
|
||||
|
|
@ -2797,7 +2834,51 @@ describe('Zod v4', () => {
|
|||
uri: 'test://nonexistent'
|
||||
}
|
||||
})
|
||||
).rejects.toThrow(/Resource test:\/\/nonexistent not found/);
|
||||
).rejects.toMatchObject({
|
||||
code: ProtocolErrorCode.ResourceNotFound,
|
||||
message: expect.stringContaining('not found')
|
||||
});
|
||||
});
|
||||
|
||||
/***
|
||||
* Test: ProtocolError for Disabled Resource
|
||||
*/
|
||||
test('should throw ProtocolError for disabled resource', async () => {
|
||||
const mcpServer = new McpServer({
|
||||
name: 'test server',
|
||||
version: '1.0'
|
||||
});
|
||||
const client = new Client({
|
||||
name: 'test client',
|
||||
version: '1.0'
|
||||
});
|
||||
|
||||
const resource = mcpServer.registerResource('test', 'test://resource', {}, async () => ({
|
||||
contents: [
|
||||
{
|
||||
uri: 'test://resource',
|
||||
text: 'Test content'
|
||||
}
|
||||
]
|
||||
}));
|
||||
|
||||
resource.disable();
|
||||
|
||||
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
||||
|
||||
await Promise.all([client.connect(clientTransport), mcpServer.server.connect(serverTransport)]);
|
||||
|
||||
await expect(
|
||||
client.request({
|
||||
method: 'resources/read',
|
||||
params: {
|
||||
uri: 'test://resource'
|
||||
}
|
||||
})
|
||||
).rejects.toMatchObject({
|
||||
code: ProtocolErrorCode.InvalidParams,
|
||||
message: expect.stringContaining('disabled')
|
||||
});
|
||||
});
|
||||
|
||||
/***
|
||||
|
|
@ -3693,7 +3774,10 @@ describe('Zod v4', () => {
|
|||
name: 'nonexistent-prompt'
|
||||
}
|
||||
})
|
||||
).rejects.toThrow(/Prompt nonexistent-prompt not found/);
|
||||
).rejects.toMatchObject({
|
||||
code: ProtocolErrorCode.InvalidParams,
|
||||
message: expect.stringContaining('not found')
|
||||
});
|
||||
});
|
||||
|
||||
/***
|
||||
|
|
|
|||
Loading…
Reference in a new issue