Skip to content

Commit 518de9e

Browse files
committed
fix: error handling in mcp-utils server handlers
1 parent 6cf3b5f commit 518de9e

File tree

1 file changed

+58
-46
lines changed

1 file changed

+58
-46
lines changed

packages/mcp-utils/src/server.ts

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -340,49 +340,61 @@ export function createMcpServer(options: McpServerOptions) {
340340
);
341341

342342
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
343-
const allResources = await getResources();
344-
const { uri } = request.params;
343+
try {
344+
const allResources = await getResources();
345+
const { uri } = request.params;
345346

346-
const resources = allResources.filter((resource) => 'uri' in resource);
347-
const resource = resources.find((resource) =>
348-
compareUris(resource.uri, uri)
349-
);
347+
const resources = allResources.filter((resource) => 'uri' in resource);
348+
const resource = resources.find((resource) =>
349+
compareUris(resource.uri, uri)
350+
);
350351

351-
if (resource) {
352-
return await resource.read(uri as `${string}://${string}`);
353-
}
352+
if (resource) {
353+
return await resource.read(uri as `${string}://${string}`);
354+
}
354355

355-
const resourceTemplates = allResources.filter(
356-
(resource) => 'uriTemplate' in resource
357-
);
358-
const resourceTemplateUris = resourceTemplates.map(({ uriTemplate }) =>
359-
assertValidUri(uriTemplate)
360-
);
356+
const resourceTemplates = allResources.filter(
357+
(resource) => 'uriTemplate' in resource
358+
);
359+
const resourceTemplateUris = resourceTemplates.map(({ uriTemplate }) =>
360+
assertValidUri(uriTemplate)
361+
);
361362

362-
const templateMatch = matchUriTemplate(uri, resourceTemplateUris);
363+
const templateMatch = matchUriTemplate(uri, resourceTemplateUris);
363364

364-
if (!templateMatch) {
365-
throw new Error('resource not found');
366-
}
365+
if (!templateMatch) {
366+
throw new Error('resource not found');
367+
}
367368

368-
const resourceTemplate = resourceTemplates.find(
369-
(r) => r.uriTemplate === templateMatch.uri
370-
);
369+
const resourceTemplate = resourceTemplates.find(
370+
(r) => r.uriTemplate === templateMatch.uri
371+
);
371372

372-
if (!resourceTemplate) {
373-
throw new Error('resource not found');
374-
}
373+
if (!resourceTemplate) {
374+
throw new Error('resource not found');
375+
}
375376

376-
const result = await resourceTemplate.read(
377-
uri as `${string}://${string}`,
378-
templateMatch.params
379-
);
377+
const result = await resourceTemplate.read(
378+
uri as `${string}://${string}`,
379+
templateMatch.params
380+
);
380381

381-
const contents = Array.isArray(result) ? result : [result];
382+
const contents = Array.isArray(result) ? result : [result];
382383

383-
return {
384-
contents,
385-
} as any;
384+
return {
385+
contents,
386+
} as any;
387+
} catch (error) {
388+
return {
389+
isError: true,
390+
content: [
391+
{
392+
type: 'text',
393+
text: JSON.stringify({ error: enumerateError(error) }),
394+
},
395+
],
396+
};
397+
}
386398
});
387399
}
388400

@@ -403,24 +415,24 @@ export function createMcpServer(options: McpServerOptions) {
403415
});
404416

405417
server.setRequestHandler(CallToolRequestSchema, async (request) => {
406-
const tools = await getTools();
407-
const toolName = request.params.name;
418+
try {
419+
const tools = await getTools();
420+
const toolName = request.params.name;
408421

409-
if (!(toolName in tools)) {
410-
throw new Error('tool not found');
411-
}
422+
if (!(toolName in tools)) {
423+
throw new Error('tool not found');
424+
}
412425

413-
const tool = tools[toolName];
426+
const tool = tools[toolName];
414427

415-
if (!tool) {
416-
throw new Error('tool not found');
417-
}
428+
if (!tool) {
429+
throw new Error('tool not found');
430+
}
418431

419-
const args = tool.parameters
420-
.strict()
421-
.parse(request.params.arguments ?? {});
432+
const args = tool.parameters
433+
.strict()
434+
.parse(request.params.arguments ?? {});
422435

423-
try {
424436
const result = await tool.execute(args);
425437
const content = result
426438
? [{ type: 'text', text: JSON.stringify(result) }]

0 commit comments

Comments
 (0)