Skip to content

Build and connect an MCP server

Expose each capability of an MCP server as a tool, resource or prompt, pick stdio or Streamable HTTP, keep a stdio server's stdout clean, and connect the server to Claude Code or the Messages API.

Scenario

Priya wraps her team's feature-flag service in an MCP server, adds it to Claude Code, and it works on the first try. Then she adds one friendly line at startup: console.log("flags server ready").

Now /mcp shows the server as failed. Nothing in her tool code changed. The line she added went to the one channel her server must keep for the protocol.

Explanation

An MCP server offers capabilities to any MCP client: Claude Code, Claude Desktop, your own agent or the Messages API. Building one means three decisions.

1. Which primitive for each capability. The question is who brings it into the conversation:

Primitive Who uses it In Claude Code Fits
Tool Claude calls it when it decides to Listed among Claude's tools Actions and live queries
Resource The user or app attaches it as context @server:protocol://path Read-only data: docs, schemas, records
Prompt The user picks it, with arguments /mcp__server__prompt Reusable task templates

2. Which transport.

  • stdio: the client launches your server as a local process and exchanges messages over its stdin and stdout. It suits single-user tools that need the local machine: files, a local database, scripts.
  • Streamable HTTP: the server runs remotely at a URL that many clients and users share, with OAuth or headers for authentication. Some older servers still use SSE.

3. Keep stdout clean. With stdio, stdout is the protocol channel. A stray print() or console.log there corrupts the message stream, and the client can fail to parse it and drop the connection. Send logs to stderr (console.error in Node, sys.stderr in Python) or to a file.

Connecting it:

  • Claude Code: claude mcp add --transport http <name> <url> for a remote server, or claude mcp add <name> -- <command> [args] for stdio. The default scope is local (just you, this project); --scope project writes a .mcp.json at the project root that you commit so the team shares it, and Claude Code asks each user to approve its servers.
  • Messages API: the MCP connector. Declare the server in mcp_servers (an https URL, a name, an optional authorization_token) and enable it with an mcp_toolset entry in tools. It supports tool calls only, and the server must be publicly reachable over HTTP, because Anthropic's servers make the call: it can't launch a local stdio server, and an internal-only server that works from Claude Code can't be reached through the connector. The connector is in beta, so send the beta header its docs page currently names (it has changed between versions). For stdio servers, resources or prompts, run your own MCP client and use the SDK's MCP helpers.

Worked example

A platform team builds a runbooks server. The first draft made everything a tool, ran over stdio on each laptop, and printed a banner to stdout.

Mapped by who uses each capability:

  • The runbook pages an engineer pulls into a conversation before asking: resources.
  • "Write an incident summary for service X", started by an engineer: a prompt with a service argument.
  • "Restart a staging deployment", which Claude decides to call: a tool.

Transport: 30 engineers share it and the runbooks live in one place, so it runs once as a remote server over Streamable HTTP instead of on every laptop. The banner is gone; logs go to stderr.

Shared through the repository with claude mcp add --transport http runbooks --scope project https://mcp.internal.example.com/mcp, which writes:

{
  "mcpServers": {
    "runbooks": {
      "type": "http",
      "url": "https://mcp.internal.example.com/mcp"
    }
  }
}