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
3 changed files with 123 additions and 23 deletions
|
|
@ -305,7 +305,7 @@ describe('createPrivateKeyJwtAuth', () => {
|
|||
|
||||
const params = new URLSearchParams();
|
||||
await expect(addClientAuth(new Headers(), params, 'https://auth.example.com/token', undefined)).rejects.toThrow(
|
||||
/Invalid character/
|
||||
/cannot be part of a valid base64|Invalid character/
|
||||
);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -233,6 +233,7 @@ export enum ProtocolErrorCode {
|
|||
InternalError = -32_603,
|
||||
|
||||
// MCP-specific error codes
|
||||
ResourceNotFound = -32_002,
|
||||
UrlElicitationRequired = -32_042
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -166,15 +166,15 @@ export class McpServer {
|
|||
);
|
||||
|
||||
this.server.setRequestHandler('tools/call', async (request, ctx): Promise<CallToolResult | CreateTaskResult> => {
|
||||
try {
|
||||
const tool = this._registeredTools[request.params.name];
|
||||
if (!tool) {
|
||||
throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Tool ${request.params.name} not found`);
|
||||
}
|
||||
if (!tool.enabled) {
|
||||
throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Tool ${request.params.name} disabled`);
|
||||
}
|
||||
const tool = this._registeredTools[request.params.name];
|
||||
if (!tool) {
|
||||
throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Tool ${request.params.name} not found`);
|
||||
}
|
||||
if (!tool.enabled) {
|
||||
throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Tool ${request.params.name} disabled`);
|
||||
}
|
||||
|
||||
try {
|
||||
const isTaskRequest = !!request.params.task;
|
||||
const taskSupport = tool.execution?.taskSupport;
|
||||
const isTaskHandler = 'createTask' in (tool.handler as AnyToolHandler<AnySchema>);
|
||||
|
|
@ -506,7 +506,7 @@ export class McpServer {
|
|||
}
|
||||
}
|
||||
|
||||
throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Resource ${uri} not found`);
|
||||
throw new ProtocolError(ProtocolErrorCode.ResourceNotFound, `Resource ${uri} not found`);
|
||||
});
|
||||
|
||||
this._resourceHandlersInitialized = true;
|
||||
|
|
|
|||
Loading…
Reference in a new issue