跪拜 Guibai
← All articles
Artificial Intelligence

MCP Server That Calls Real HTTP APIs: Error Handling and What Actually Matters

By 陆枫Larry ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

MCP servers that only compute a + b are demos, not tools. This walkthrough shows the practical wiring for letting an AI assistant hit real endpoints, and it makes the boundary between MCP transport errors and HTTP business errors explicit, which is where a lot of confusion happens when debugging.

Summary

The second MCP practice session moves past the basic math tool and into a real HTTP integration. A developer builds a userServer with the MCP TypeScript SDK, registers a getUserInfo tool with Zod input validation, and connects it to Copilot through VS Code's stdio configuration. Copilot then asks the tool to query JSONPlaceholder's public user endpoint, and the full loop works end to end.

The interesting part is error handling. HTTP 404 gets mapped to an isError result with a human-readable message. Connection failures are caught via try/catch, and AbortSignal.timeout(5000) adds a 5-second timeout. The tests cover three cases: successful query, missing user, and network failure. A regression check confirms the normal path still works after the error handling changes.

The write-up also separates two layers clearly: an HTTP failure is not an MCP communication failure. The MCP channel stays healthy while the underlying API call fails. The author notes that MCP doesn't understand natural language, the AI model does, and that MCP and HTTP APIs complement each other rather than compete. Existing backend interfaces can be wrapped as MCP tools without rewriting business logic.

Takeaways
— A tool registered with the MCP TypeScript SDK can use plain fetch() inside its handler, so any existing HTTP API can be wrapped as an MCP tool.
— The inputSchema from Zod does validation, not coercion: z.number() rejects a string like "123" unless you explicitly use z.coerce.number().
— Returning isError: true with a text message tells the MCP client that the tool failed, which prevents Copilot from hallucinating a nonexistent user.
— A 404 response from an HTTP API is a business error, not an MCP protocol error; the MCP communication itself stays healthy.
— Connection failures throw exceptions from fetch(), so try/catch is required, and AbortSignal.timeout(5000) gives the request a 5-second cut-off.
— After adding error handling, the normal code path should be retested; a regression check confirmed the successful query still works.
— MCP Server processes started with tsx and stdio require a manual restart after source changes; no hot reload is built in.
— An MCP Server is simultaneously a server to the AI client and a client to external HTTP APIs; these roles are relative to each communication.
Conclusions

The author's step of curl-testing the HTTP endpoint before wiring it into MCP is the quiet hero of the article: it separates HTTP-layer from MCP-layer debugging, so a failure later narrows down quickly.

Treating isError as a first-class return value is what keeps an AI assistant honest. Without it, Copilot might just invent data instead of reporting a 404.

The distinction between validation and coercion in Zod matters more in MCP than in typical UI code, because the AI model may produce string values from natural language, and silent coercion can mask type bugs.

Parameter definitions double as discovery metadata for clients, which means a well-described schema isn't just for validation; it guides the model's tool selection.

MCP tools need the same engineering discipline as any backend endpoint, and the author's list of missing pieces (auth, permissions, field filtering) is a realistic checklist before anything goes to production.

Concepts & terms
MCP (Model Context Protocol)
An open protocol that standardizes how AI applications discover and call external tools, data sources, and APIs. In this article, an MCP Server wraps a user lookup as a tool that Copilot can invoke via a client.
serveStdio
A helper from the MCP TypeScript SDK that runs the MCP Server over standard input/output (stdio), letting a local process like VS Code talk to it without opening a network port.
AbortSignal.timeout()
A Web API method that returns an AbortSignal which automatically aborts a fetch() request after a specified number of milliseconds. Used here to enforce a 5-second timeout on the HTTP call made inside the MCP tool.
package-lock.json
A file generated by npm that records the exact resolved versions of every dependency in a project. It enables reproducible installs via `npm ci`, so all developers get the same dependency tree.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗