The short answer

#

MCP (Model Context Protocol) is an open standard for connecting AI clients to tools and data. You write one small server that exposes what you own, and any MCP-aware client can use it: Claude Code, desktop chat apps, agent frameworks and the Claude API's remote connector. Build a server when you own data or an API that several of those clients should reach. If you only need a function inside your own app, a plain tool definition in your own code is simpler.


_12
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
_12
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
_12
import { z } from 'zod';
_12
_12
const server = new McpServer({ name: 'orders', version: '1.0.0' });
_12
_12
server.tool('get_order', 'Look up one order by its ID.', { orderId: z.string() }, async ({ orderId }) => {
_12
const order = await db.orders.find(orderId);
_12
return { content: [{ type: 'text', text: JSON.stringify(order) }] };
_12
});
_12
_12
await server.connect(new StdioServerTransport());

What it means

#

Before MCP, every AI product had its own way of plugging in tools. If you wanted your internal API available in two chat clients and your own app, you wrote three integrations. MCP replaces that with a shared contract, in the same way a language server protocol lets one TypeScript language server work in every editor.

The protocol has two sides. A client lives inside the AI application and talks to the model. A server is a small process you run that advertises what it offers. They speak JSON-RPC over a transport: stdio for a local server the client launches as a child process, or HTTP for a remote server at a URL.

A server can expose three kinds of thing:

  • Tools are functions the model may call, each with a name, a description and an input schema. They map almost one to one onto the tool definitions you would pass to the Messages API.
  • Resources are pieces of data the client can read and attach to the context, such as a file, a database row or a config, addressed by a URI.
  • Prompts are reusable prompt templates the client can offer to the user, often surfaced as slash commands.

Tools are what most servers ship, and what most clients support best. The client discovers them with a tools/list call, shows them to the model, and when the model asks for one the client sends tools/call to your server and feeds the result back. From the model's point of view this is ordinary tool use; MCP is just where the tool lives.

How to choose

#

Build a server when several clients need the same thing

The case for a server is reuse. You have an orders database, a ticketing API or an internal knowledge base, and you want it reachable from Claude Code while developing, from a desktop chat app for the support team, and from an agent you run in production. One server, three consumers, one place to fix bugs and enforce permissions.

It also makes sense when the thing you expose is not your app at all. A server wrapping your company's design system, your feature flag service or your analytics warehouse is a product in its own right, and MCP gives it a distribution channel.

Stick with a tool definition when it is only your app

If you are building a chat feature inside your Next.js app and the tool is "look up this user's orders", skip MCP. Define the tool in the tools array of your API call, run the function in your route handler, and send the result back. There is no second process, no transport, nothing to deploy. You can always lift the function into an MCP server later; the schema carries across almost unchanged.

Connect a local server to Claude Code

For a stdio server, the client launches your process. In Claude Code, a project-scoped .mcp.json is enough:


_10
{
_10
"mcpServers": {
_10
"orders": { "command": "node", "args": ["dist/server.js"] }
_10
}
_10
}

Compile the TypeScript first, and avoid writing to stdout in the server; that channel carries the protocol, so logs go to stderr.

Connect a remote server from the API

The Messages API can call a remote MCP server directly, so your production agent uses the same server as your editor. The server must be reachable over HTTP, not stdio, and the feature sits behind a beta flag (check the docs for the current one).


_10
const response = await client.beta.messages.create({
_10
model: 'claude-sonnet-5-5',
_10
max_tokens: 4096,
_10
betas: ['mcp-client-2025-11-20'],
_10
mcp_servers: [{ type: 'url', url: 'https://mcp.example.com/mcp', name: 'orders' }],
_10
tools: [{ type: 'mcp_toolset', mcp_server_name: 'orders' }],
_10
messages: [{ role: 'user', content: 'Where is order ORD-10422?' }],
_10
});

Both halves are required: the mcp_servers entry says where the server is, and the mcp_toolset entry says the model may use its tools.

Treat the server as a security boundary

A server runs with whatever permissions you give its process. A tool called run_sql that accepts raw SQL is a tool for deleting your database, because the model will eventually be asked to by a user, or by text hidden in a document it reads. Expose narrow, named operations instead: get_order, list_open_tickets, create_draft_reply. Validate inputs with the schema, use a read-only database role where you can, and make anything destructive require confirmation on the client side. Remember that tool results are untrusted input too; a web page your server fetches can contain instructions aimed at the model.

Common pitfalls

#
  • Writing to stdout. With the stdio transport, a stray console.log corrupts the protocol stream and the client disconnects. Use console.error for logging.
  • Vague tool descriptions. The model chooses tools by reading them. "Get data" gets ignored or misused; "Look up one order by its ID, returns status and delivery date" gets called correctly.
  • Exposing a general-purpose tool. Shell access, raw SQL or arbitrary HTTP turn a helpful server into an attack surface. Expose the five operations people actually need.
  • Returning enormous payloads. Every byte you return becomes input tokens on every later turn. Trim to the fields the model needs and paginate lists.
  • Building a server when a function would do. If only your own app will call it, MCP adds a process, a transport and a deployment for no gain.
  • Assuming every client supports every feature. Tools are universally supported; resources and prompts less so. Check your target client before you rely on them.
#

quick question

Was this resource helpful?