Skip to content

fix(mcp): wrap tools/call result in spec-required content array - #2

Open
anticafeformat-dev wants to merge 1 commit into
stemirkhan:mainfrom
anticafeformat-dev:fix/mcp-spec-tool-call-content-wrap
Open

anticafeformat-dev wants to merge 1 commit into
stemirkhan:mainfrom
anticafeformat-dev:fix/mcp-spec-tool-call-content-wrap

Conversation

@anticafeformat-dev

Copy link
Copy Markdown

Problem

callTool in src/mcp/server.ts returns the tool handler's payload as the JSON-RPC result directly:

const result: unknown = await tool.handler(args);
return result;

For example, get_current_user ends up sending:

{"jsonrpc":"2.0","id":2,"result":{"user":{"id":1,"name":"...","email":"..."}}}

The MCP specification requires a tools/call response to use the standard content envelope:

{
  "content": [{"type": "text", "text": "..."}],
  "isError": false
}

Strict MCP clients (e.g., Claude Code) ignore non-spec result shapes and surface tool calls as (completed with no output) — silent failure with no error to debug. This affects all 13 registered tools, not a specific one.

Fix

Wrap the handler's return value in the required envelope at a single point — the central callTool method — so every tool gets spec-compliant output without per-tool changes:

const result: unknown = await tool.handler(args);
return {
  content: [{ type: "text", text: JSON.stringify(result) }]
};

Verification

stdin smoke test (initialize + tools/call get_current_user):

  • Before:
    {"jsonrpc":"2.0","id":2,"result":{"user":{"id":1,"name":"...","email":"..."}}}
  • After:
    {"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"text","text":"{\"user\":{\"id\":1,\"name\":\"...\"}}"}]}}

Claude Code now displays the JSON payload to the model; previously returned silent (no output).

Note

This is an independent fix from #1 (capabilities shape). Both are MCP-spec compliance issues but in different code paths and can be merged independently.

`callTool` in `src/mcp/server.ts` returned the tool handler's payload
as the JSON-RPC `result` directly (e.g. `{user: {...}}` for
`get_current_user`). The MCP specification requires `tools/call` to
return:

```json
{
  "content": [{"type": "text", "text": "..."}],
  "isError": false   // optional, defaults to false
}
```

Strict MCP clients (e.g. Claude Code) ignore non-spec result shapes
and surface tool calls as `(completed with no output)` — silent
failure with no error to debug. Affects all 13 registered tools.

This patch wraps the handler's return value in the required envelope
at a single point (the central `callTool` method), so every tool gets
spec-compliant output without per-tool changes.

## Verification

stdin smoke test (`initialize` + `tools/call get_current_user`):

- Before:
  `{"result":{"user":{"id":1,"name":"Григорий",...}}}`
- After:
  `{"result":{"content":[{"type":"text","text":"{\"user\":{\"id\":1,...}}"}]}}`

Claude Code now displays the JSON payload to the model; previously
returned `(no output)`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant