· via dev.to (home feed)
Connecting MCP servers straight into a custom Claude agent loop
A dev.to tutorial shows how to run MCP servers over stdio and HTTP as tools inside a hand-written Claude agent loop in TypeScript, covering tool filtering, connection reuse and a prompt-caching pitfall.

What the tutorial covers
A tutorial published on dev.to explains how to attach Model Context Protocol (MCP) servers — filesystems, databases, GitHub, internal APIs — to a Claude agent loop you build yourself in TypeScript, rather than writing a wrapper per tool. The example uses the official MCP client SDK alongside Anthropic's SDK, and assumes you already have a loop that accepts a set of tool definitions plus a function that executes a tool by name. The end state: MCP tools and hand-written tools share one registry, and the loop cannot tell them apart.
Two places to put the MCP client
Before any code, the post separates two integration points. You can run the MCP client inside your own server process, which supports both stdio (local child processes) and HTTP, or you can use the MCP connector built into Anthropic's API — an mcp_servers field on the request paired with an mcp_toolset entry in tools, requiring a beta header — which reaches remote HTTP servers only.
According to the post, the API connector means less code when a public remote server is all you need. Running the client yourself fits the other cases: local stdio servers, servers on private networks, and any setup where you want a hand on each call. Because tool execution happens in your loop rather than inside the API request, you can log, meter, gate by user, and count calls against a plan.
Config, naming and filtering
Servers are declared as JSON in one environment variable, so adding one is a matter of editing configuration rather than touching code. Entries carrying a command run as child processes that communicate over stdio; entries carrying a URL are reached over Streamable HTTP, with optional auth headers. The config is validated with zod so a mistake is caught when the process boots rather than when a tool is first invoked. Server names are restricted to characters valid in a Claude tool name, because the name becomes part of every tool the server exposes.
The connection routine picks a transport, connects an SDK client, lists the server's tools, and registers each one under a name of the form server__tool, capped at 64 characters to respect Claude's tool-name limit. The prefix keeps names unique when two servers expose the same tool and makes each call's destination visible in logs. MCP's inputSchema is already JSON Schema, the shape Claude's input_schema takes, so it needs no conversion; your own tools can keep using zod and translate on the way in.
The post flags two further decisions. An explicit per-server allow list is where the permission line gets drawn: the filesystem server can write and move files, so a read-only assistant should expose only its reading tools, and the author's advice is to enumerate allowed tools by default for anything capable of mutating data. On errors, a result marked isError by MCP is thrown, which the loop converts into a tool_result with is_error set, so Claude sees what failed and can try a different approach instead of the whole run aborting. Since MCP returns a list of content blocks, the post flattens text and resource blocks into a string for the model, and pipes child-process stderr so a noisy server cannot write into the application's logs.
Connect once per process
Launching a local stdio server is not free, and each HTTP connection costs a handshake, so the post caches the connection promise and awaits it at the top of the loop: the first request absorbs the cost, and later ones skip it. Servers connect in parallel, and a failure in one is caught and logged, leaving the rest running. On serverless platforms, per-process means per cold start, which the post considers fine for HTTP servers but costly for stdio, since relaunching npx on every cold start is slow; a long-running Node process is recommended there.
Tool order and prompt caching
A later section of the post zeroes in on a detail with a price tag. Claude serializes tool definitions at the head of each request, ahead of the system prompt, and prompt caching keys on an exact prefix match. If two requests present the tool list in a different sequence, both miss the cache, across all users. MCP invites exactly this bug: parallel connections finish in the order the network happens to deliver, so a registry populated in connection order can differ from process to process. The remedy is to sort the list, for instance by tool name, when building the definitions sent to the API.
Why it matters
Most MCP coverage assumes a host application — Claude Desktop, an IDE, or the API's own connector. For teams building their own agents, this dev.to post is a worked example of the other path: the protocol's client SDK is ordinary enough to embed in a hand-written loop, and the operational details it surfaces — permission filtering, error mapping, connection reuse, cache-stable tool ordering — are what separate a multi-server agent that stays cheap and reliable in production from one that quietly is not. The caching point generalizes beyond MCP, too: any agent whose tool list changes order between requests pays full token price for it.
- #mcp
- #ai-agents
- #claude
- #typescript
- #anthropic-api