Skip to main content

How to build a Model Context Protocol (MCP) server

Build an MCP server that exposes tools and resources to AI assistants: scaffold with the SDK, register tools and resources, choose a transport, and test with an MCP client.

Difficulty
Intermediate
Duration
55 minutes
Steps
6

What MCP is

The Model Context Protocol (MCP) is an open standard that lets AI assistants connect to external tools and data through a uniform interface. Instead of building a custom integration for every assistant, you expose a single MCP server. A host application connects an MCP client to your server, discovers its tools and resources, and lets the model use them.

MCP defines two main capabilities: tools (functions the model can call) and resources (data the model can read).

Prerequisites

  • Node.js 18+ or Python 3.10+
  • An MCP-capable client to test against
  • A use case: data or actions you want to expose

Steps

1. Understand MCP servers and clients

A server advertises capabilities; a client, embedded in a host app, calls them. Communication uses JSON-RPC messages over a transport.

2. Scaffold the server

Install the MCP SDK and create a server instance.

npm install @modelcontextprotocol/sdk
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
const server = new McpServer({ name: "my-server", version: "1.0.0" });

3. Define and register tools

A tool has a name, an input schema, and a handler. The schema and description tell the model when and how to use it.

server.tool("get_user", { id: z.string() }, async ({ id }) => {
  return { content: [{ type: "text", text: await lookup(id) }] };
});

4. Expose resources

Resources are addressable data the model can read, such as files or records, identified by a URI.

5. Choose a transport

For local tools use stdio; the host launches your server as a subprocess. For remote servers use a streamable HTTP transport.

6. Connect and test with a client

Register the server in an MCP client's configuration, then confirm the client lists your tools and can invoke them.

Verification

Start the server and connect a client. Confirm the client discovers your tools and resources. Invoke a tool from the client and verify the handler runs and the result returns. Trigger an invalid argument and confirm a clear error is reported.

Next Steps

Add authentication for remote transports, validate every input, log tool invocations, and publish the server so other assistants can use it.

Prerequisites

  • Node.js or Python installed
  • Familiarity with JSON RPC concepts
  • Understanding of LLM tool use

Steps

  • 1
    Understand MCP servers and clients
  • 2
    Scaffold the server
  • 3
    Define and register tools
  • 4
    Expose resources
  • 5
    Choose a transport
  • 6
    Connect and test with a client

Category

AI ML