Answers you can trust, from Codeables
Every page on Codeables is structured and verified — built so people and the AI agents they rely on can trust it. Explore more from the source behind this answer.
Explore CodeablesMCP in Node.js: best MCP client/server libraries and hosted tool registries to avoid custom integrations
Most teams adopting MCP in Node.js want the same thing: call a rich set of tools and services without writing one-off, brittle integrations for every provider. You want a clean MCPClient in your agent, an MCPServer exposing your own tools, and ideally a hosted tool registry so you can “plug in” capabilities instead of reinventing them.
Quick Answer: In Node.js, the leading MCP client/server options are Mastra’s
MCPClient/MCPServer(TypeScript-native, agent‑first), the official Anthropic@modelcontextprotocol/sdk, and gateway-style services like Portkey MCP. Pair these with emerging hosted registries (e.g., Portkey’s MCP hub, experimental MCP marketplaces, and OSS registries you host yourself) to avoid building custom integrations for every tool.
Frequently Asked Questions
What’s the best way to use MCP with Node.js agents without drowning in custom integrations?
Short Answer: Use a TypeScript-native MCP client/server (Mastra or Anthropic’s SDK) to normalize tool access, and lean on hosted MCP registries or gateways where possible so tools feel like a shared “plugin layer,” not bespoke code.
Expanded Explanation:
In Node.js, MCP is less about a single “magic” library and more about choosing a stack that gives you three things: a solid MCPClient for calling tools, an MCPServer for exposing tools, and a way to register many tools without hand-wiring every integration. Mastra’s MCPClient and MCPServer live right inside your agent and workflow code, so tools become infrastructure primitives in your TypeScript app. The official @modelcontextprotocol/sdk gives you lower-level building blocks if you want to wire your own orchestration.
To avoid custom integrations, use MCP as the universal contract: treat HTTP APIs, databases, and internal services as MCP tools, then optionally register external tools via a hosted registry (e.g., Portkey MCP) or a self-hosted “tool hub” server. Your Node.js agents only need to know “call MCP,” not “call Vendor X with header Y and auth Z.”
Key Takeaways:
- Pick an MCP client/server that fits your agent framework (Mastra for TypeScript agents, Anthropic SDK for lower-level control).
- Reduce custom integrations by treating MCP as your universal plugin layer and using registries/gateways to aggregate tools.
How do I set up an MCP client and server in a Node.js / TypeScript project?
Short Answer: Install a Node-compatible MCP SDK, create an MCPServer that exposes tools, then configure an MCPClient in your agent runtime to connect to one or more servers (local, HTTP, or gateway).
Expanded Explanation:
In practice, you’ll usually start by exposing something you already have—like a REST API or internal service—as an MCPServer. Then you plug an MCPClient into your agent or workflow so it can call that MCPServer as a tool. In Mastra, MCPClient and MCPServer are first-class primitives: you define tools in TypeScript, run them locally in the dev server, and expose them over HTTP(S) or other transports. The client is then attached to an Agent in your Workspace.
If you’re not using Mastra, the process is similar with the Anthropic SDK: implement an MCP server in Node.js, define tools and schemas, then configure your runtime or gateway to talk to that server. The model sees a unified tool surface; you keep type safety, logging, and execution control in your Node app.
Steps:
-
Install MCP packages
- Mastra stack:
npm install @mastra/core @mastra/mcp - Anthropic SDK:
npm install @modelcontextprotocol/sdk
- Mastra stack:
-
Define and run an MCPServer (Node/TS)
With Mastra:import { MCPServer, mcpTool } from '@mastra/mcp'; const getUserTool = mcpTool({ name: 'get_user', description: 'Fetch a user by ID', inputSchema: { type: 'object', properties: { id: { type: 'string' } }, required: ['id'], }, execute: async ({ id }) => { // Call your DB or API here const user = await fetch(`https://api.example.com/users/${id}`).then(r => r.json()); return { id: user.id, email: user.email }; }, }); const server = new MCPServer({ name: 'user-service', tools: [getUserTool], }); server.listen({ port: 4000 }); -
Attach an MCPClient to your agent runtime
With Mastra:import { Agent, Workspace } from '@mastra/core'; import { MCPClient } from '@mastra/mcp'; const mcpClient = new MCPClient({ servers: [ { id: 'user-service', url: 'http://localhost:4000', // or gateway URL }, ], }); const agent = new Agent({ name: 'support-agent', model: 'gpt-4o-mini', tools: [mcpClient], // tools exposed via MCP }); const workspace = new Workspace({ agents: [agent] }); const result = await workspace.run('support-agent', { input: 'Look up user 123 and confirm their email.', }); console.log(result.output);
What are the main differences between MCP libraries for Node.js (Mastra vs Anthropic SDK vs gateways like Portkey)?
Short Answer: Mastra’s MCP primitives are opinionated and agent-first for TypeScript apps, the Anthropic SDK is lower-level and model-agnostic, and gateways like Portkey MCP centralize tool routing and hosted registries.
Expanded Explanation:
If you want MCP tightly integrated with your agent and workflows, Mastra’s MCPClient/MCPServer give you direct TypeScript ergonomics, observability, and shared tooling with agents, workflows, and memory. You stay inside one framework, and your MCP tools sit next to your Agent definitions.
Anthropic’s @modelcontextprotocol/sdk is a great choice if you want to work closer to the protocol or build framework-agnostic MCP servers/clients. You’ll wire your own observability, orchestration, and error handling, but you get a canonical implementation of the spec.
Portkey and similar gateways add a deployment and routing layer. They handle multi-model routing, secrets, and, increasingly, hosted MCP tool registries. Instead of your client connecting directly to N servers, you connect once to the gateway and let it fan out.
Comparison Snapshot:
- Option A: Mastra
MCPClient/MCPServer- TypeScript-native, integrated with agents, workflows, and observability.
- Tools live in your codebase; expose over HTTP(S) or attach locally.
- Option B: Anthropic
@modelcontextprotocol/sdk+ Portkey MCP or your own glue- Lower-level, spec-centric tooling; you bring your own agent framework and routing.
- Good if you want complete control or are building your own framework/gateway.
- Best for:
- Mastra: product teams shipping Node.js/TypeScript agents into real services (Next.js, Express, Hono) and wanting MCP as part of their infrastructure.
- Anthropic SDK + gateway: platform teams building custom orchestration, multi-language MCP ecosystems, or internal tool marketplaces.
How do I actually plug into hosted MCP tool registries or hubs from Node.js?
Short Answer: Point your MCPClient at a gateway or hosted MCP hub that exposes many tools via a single endpoint, then treat that hub as one server in your Node.js agent runtime.
Expanded Explanation:
Hosted MCP registries act like “app stores” for tools: you get a curated list of MCP servers (for SaaS APIs, internal services, etc.) behind a single URL or config. From Node.js, your job is to connect once and let the registry handle discovery and routing. Some gateways (e.g., Portkey MCP) provide a catalog plus configuration (auth, rate limiting, per-tool settings). For more control, you can build and host your own registry as a dedicated MCPServer that proxies out to other MCP servers and HTTP APIs.
In Mastra, you treat the registry like any other MCP server: define a servers entry in your MCPClient that points to the registry URL. The registry may support additional APIs for filtering or listing tools, but your agent just sees a tool list exposed by MCP.
What You Need:
- A hosted MCP hub or gateway URL (e.g., Portkey MCP endpoint or your internal MCP registry service).
- A Node.js MCPClient configuration (Mastra or Anthropic SDK) that can talk to that endpoint.
Example with Mastra:
import { MCPClient } from '@mastra/mcp';
const mcpClient = new MCPClient({
servers: [
{
id: 'hosted-registry',
url: process.env.MCP_REGISTRY_URL!, // e.g. https://mcp.portkey.ai
headers: {
Authorization: `Bearer ${process.env.MCP_REGISTRY_TOKEN}`,
},
},
],
});
// Then attach to your Agent as in the earlier example
Strategically, how should I design my Node.js MCP stack to avoid long-term integration debt?
Short Answer: Treat MCP as your standard interface for tools, keep Node.js agents thin, centralize tools behind MCP servers/registries, and invest early in observability and evals so you can change tools without breaking behavior.
Expanded Explanation:
Long-term, the risk isn’t “which SDK did I pick,” it’s “did I bake vendor-specific details into my agent logic?” If your Node.js agents call APIs directly, every new SaaS or internal service adds bespoke code paths, auth, and error modes. If you instead funnel everything through MCP, tools become composable and replaceable: swap out a CRM provider, add a new RAG backend, or move from a custom HTTP service to a more robust MCP server with minimal agent changes.
With Mastra, you can define tools as MCP once, attach them to agents, and monitor everything via Observability. You also get evals to track behavior as you change tools or providers. That means you can treat MCP tool registries and gateways as infrastructure knobs, not reasons to rewrite code.
Why It Matters:
- Reduces integration sprawl: MCP turns “many bespoke integrations” into “one protocol, many tools,” so your Node.js code stays focused on orchestration, not glue.
- Improves reliability and change velocity: With tracing and evals in place, you can swap tools, introduce a hosted registry, or refactor servers while verifying behavior and cost in production.
Quick Recap
In Node.js, the most sustainable way to use MCP is to: (1) pick a strong MCP client/server library—Mastra’s MCPClient/MCPServer for TypeScript agents or Anthropic’s SDK for lower-level work; (2) expose your own tools and services via MCP instead of ad-hoc HTTP integrations; and (3) connect to hosted MCP registries or gateways so you can add and manage tools without rewriting your agents. Wrap this in observability and evals, and MCP becomes a stable plugin layer inside your Node infrastructure, not another integration to babysit.