Skip to content

Commit a470a44

Browse files
betegonclaude
andcommitted
feat(core): support registerTool/registerResource/registerPrompt in MCP integration
The @modelcontextprotocol/sdk introduced register* methods alongside the legacy tool/resource/prompt API in 1.x, and made them the only option in 2.x. - MCPServerInstance now accepts both old and new method names - validateMcpServerInstance accepts servers with either API set - wrapAllMCPHandlers instruments whichever methods are present - captureHandlerError maps register* names to the same error categories Co-Authored-By: claude-sonnet-4-6 <noreply@anthropic.com>
1 parent b75e657 commit a470a44

File tree

4 files changed

+46
-23
lines changed

4 files changed

+46
-23
lines changed

packages/core/src/integrations/mcp-server/handlers.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function captureHandlerError(error: Error, methodName: keyof MCPServerInstance,
9696
try {
9797
const extraData: Record<string, unknown> = {};
9898

99-
if (methodName === 'tool') {
99+
if (methodName === 'tool' || methodName === 'registerTool') {
100100
extraData.tool_name = handlerName;
101101

102102
if (
@@ -114,10 +114,10 @@ function captureHandlerError(error: Error, methodName: keyof MCPServerInstance,
114114
} else {
115115
captureError(error, 'tool_execution', extraData);
116116
}
117-
} else if (methodName === 'resource') {
117+
} else if (methodName === 'resource' || methodName === 'registerResource') {
118118
extraData.resource_uri = handlerName;
119119
captureError(error, 'resource_execution', extraData);
120-
} else if (methodName === 'prompt') {
120+
} else if (methodName === 'prompt' || methodName === 'registerPrompt') {
121121
extraData.prompt_name = handlerName;
122122
captureError(error, 'prompt_execution', extraData);
123123
}
@@ -127,31 +127,39 @@ function captureHandlerError(error: Error, methodName: keyof MCPServerInstance,
127127
}
128128

129129
/**
130-
* Wraps tool handlers to associate them with request spans
130+
* Wraps tool handlers to associate them with request spans.
131+
* Instruments both `tool` (legacy API) and `registerTool` (new API) if present.
131132
* @param serverInstance - MCP server instance
132133
*/
133134
export function wrapToolHandlers(serverInstance: MCPServerInstance): void {
134-
wrapMethodHandler(serverInstance, 'tool');
135+
if (typeof serverInstance.tool === 'function') wrapMethodHandler(serverInstance, 'tool');
136+
if (typeof serverInstance.registerTool === 'function') wrapMethodHandler(serverInstance, 'registerTool');
135137
}
136138

137139
/**
138-
* Wraps resource handlers to associate them with request spans
140+
* Wraps resource handlers to associate them with request spans.
141+
* Instruments both `resource` (legacy API) and `registerResource` (new API) if present.
139142
* @param serverInstance - MCP server instance
140143
*/
141144
export function wrapResourceHandlers(serverInstance: MCPServerInstance): void {
142-
wrapMethodHandler(serverInstance, 'resource');
145+
if (typeof serverInstance.resource === 'function') wrapMethodHandler(serverInstance, 'resource');
146+
if (typeof serverInstance.registerResource === 'function') wrapMethodHandler(serverInstance, 'registerResource');
143147
}
144148

145149
/**
146-
* Wraps prompt handlers to associate them with request spans
150+
* Wraps prompt handlers to associate them with request spans.
151+
* Instruments both `prompt` (legacy API) and `registerPrompt` (new API) if present.
147152
* @param serverInstance - MCP server instance
148153
*/
149154
export function wrapPromptHandlers(serverInstance: MCPServerInstance): void {
150-
wrapMethodHandler(serverInstance, 'prompt');
155+
if (typeof serverInstance.prompt === 'function') wrapMethodHandler(serverInstance, 'prompt');
156+
if (typeof serverInstance.registerPrompt === 'function') wrapMethodHandler(serverInstance, 'registerPrompt');
151157
}
152158

153159
/**
154-
* Wraps all MCP handler types (tool, resource, prompt) for span correlation
160+
* Wraps all MCP handler types for span correlation.
161+
* Supports both the legacy API (`tool`, `resource`, `prompt`) and the newer API
162+
* (`registerTool`, `registerResource`, `registerPrompt`), instrumenting whichever methods are present.
155163
* @param serverInstance - MCP server instance
156164
*/
157165
export function wrapAllMCPHandlers(serverInstance: MCPServerInstance): void {

packages/core/src/integrations/mcp-server/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const wrappedMcpServerInstances = new WeakSet();
1414
/**
1515
* Wraps a MCP Server instance from the `@modelcontextprotocol/sdk` package with Sentry instrumentation.
1616
*
17-
* Compatible with versions `^1.9.0` of the `@modelcontextprotocol/sdk` package.
17+
* Compatible with versions `^1.9.0` of the `@modelcontextprotocol/sdk` package (legacy `tool`/`resource`/`prompt` API)
18+
* and versions that expose the newer `registerTool`/`registerResource`/`registerPrompt` API (introduced in 1.x, sole API in 2.x).
1819
* Automatically instruments transport methods and handler functions for comprehensive monitoring.
1920
*
2021
* @example

packages/core/src/integrations/mcp-server/types.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,29 @@ export type JsonRpcMessage = JsonRpcRequest | JsonRpcNotification | JsonRpcRespo
8787

8888
/**
8989
* MCP server instance interface
90-
* @description MCP server methods for registering handlers
90+
* @description MCP server methods for registering handlers.
91+
* Supports both the legacy API (`tool`, `resource`, `prompt`) used in SDK 1.x
92+
* and the newer API (`registerTool`, `registerResource`, `registerPrompt`) introduced in SDK 1.x
93+
* and made the only option in SDK 2.x.
9194
*/
9295
export interface MCPServerInstance {
93-
/** Register a resource handler */
94-
resource: (name: string, ...args: unknown[]) => void;
96+
/** Register a resource handler (legacy API) */
97+
resource?: (name: string, ...args: unknown[]) => void;
9598

96-
/** Register a tool handler */
97-
tool: (name: string, ...args: unknown[]) => void;
99+
/** Register a tool handler (legacy API) */
100+
tool?: (name: string, ...args: unknown[]) => void;
98101

99-
/** Register a prompt handler */
100-
prompt: (name: string, ...args: unknown[]) => void;
102+
/** Register a prompt handler (legacy API) */
103+
prompt?: (name: string, ...args: unknown[]) => void;
104+
105+
/** Register a resource handler (new API, SDK >=1.x / 2.x) */
106+
registerResource?: (name: string, ...args: unknown[]) => void;
107+
108+
/** Register a tool handler (new API, SDK >=1.x / 2.x) */
109+
registerTool?: (name: string, ...args: unknown[]) => void;
110+
111+
/** Register a prompt handler (new API, SDK >=1.x / 2.x) */
112+
registerPrompt?: (name: string, ...args: unknown[]) => void;
101113

102114
/** Connect the server to a transport */
103115
connect(transport: MCPTransport): Promise<void>;

packages/core/src/integrations/mcp-server/validation.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,20 @@ export function isJsonRpcResponse(message: unknown): message is JsonRpcResponse
5757
}
5858

5959
/**
60-
* Validates MCP server instance with type checking
60+
* Validates MCP server instance with type checking.
61+
* Accepts both the legacy API (`tool`, `resource`, `prompt`) used in SDK 1.x
62+
* and the newer API (`registerTool`, `registerResource`, `registerPrompt`) introduced
63+
* alongside the legacy API in SDK 1.x and made the only option in SDK 2.x.
6164
* @param instance - Object to validate as MCP server instance
6265
* @returns True if instance has required MCP server methods
6366
*/
6467
export function validateMcpServerInstance(instance: unknown): boolean {
6568
if (
6669
typeof instance === 'object' &&
6770
instance !== null &&
68-
'resource' in instance &&
69-
'tool' in instance &&
70-
'prompt' in instance &&
71-
'connect' in instance
71+
'connect' in instance &&
72+
(('tool' in instance && 'resource' in instance && 'prompt' in instance) ||
73+
('registerTool' in instance && 'registerResource' in instance && 'registerPrompt' in instance))
7274
) {
7375
return true;
7476
}

0 commit comments

Comments
 (0)