all writing

Model Context Protocol

How to Build an MCP Server: A Practical Guide to the Model Context Protocol

2026-09-16 · by Talha Jaleel

How to build an MCP server guide cover

The Model Context Protocol (MCP) went from a new Anthropic standard to something every major AI coding tool supports, with thousands of servers now published, because it solves a real problem: instead of hard-coding a custom integration between every AI application and every tool or data source, MCP gives them a common language. This guide covers what an MCP server actually is, the three things it exposes, how to build one, and the security decisions that matter once an AI can call your tools.

What MCP Is and Why It Exists

The Model Context Protocol is an open standard that lets an AI application discover and use external tools, data, and prompts through a single, consistent interface. It is a JSON-RPC based protocol, and the common analogy is that MCP is USB-C for AI applications: one standard connector, so any compliant client can talk to any compliant server without a bespoke integration for each pairing.

The problem it solves is combinatorial. Before MCP, wiring an AI assistant to your database, your ticketing system, and your internal docs meant writing and maintaining a separate integration for each one, inside each AI tool you used. MCP turns that into a single server per tool that every MCP-aware client (Claude, IDEs, and other AI applications) can use, so the integration is written once and reused everywhere.

This matters most for teams building agents. An AI agent is only as useful as the tools it can call, and MCP lets you expose those tools in a framework-neutral way, so the same server works whether the agent is built on LangGraph, CrewAI, or anything else (a portability benefit covered in the agent frameworks comparison).

The Three Things an MCP Server Exposes

Tools are the most important primitive: functions the AI can call to take an action or fetch live data, such as querying a database, creating a ticket, or looking up an order. Each tool has a name, a description, and a typed input schema, and the quality of that description matters more than people expect, because the model decides whether and how to call a tool based on what the description says it does.

Resources are read-only data the server can expose for context, such as files, documents, or records, that the client can pull in without the model having to call a tool. Where tools are for actions and live lookups, resources are for handing the model reference material, and they are addressed by URIs so the client can request exactly the ones it needs.

Prompts are reusable, parameterized templates the server offers, so a server can ship well-designed prompt flows (a code-review prompt, a summarization prompt) that clients can invoke by name. Most servers lean heavily on tools, but resources and prompts round out the protocol so a server can provide data and guided workflows, not just actions.

Building a Server: Transport, JSON-RPC, and the stdout Gotcha

An MCP server is a program that speaks the protocol over a transport. The two transports are stdio (the client launches your server as a local subprocess and talks to it over standard input and output) and HTTP (the server runs as a remote service the client connects to over the network). Official SDKs exist for Python, TypeScript, and other languages, and they handle the JSON-RPC framing so you implement handlers, not wire format.

The canonical starter is a small server that exposes a couple of tools: the official tutorial builds a weather server with get_alerts and get_forecast handlers. In practice you define each tool with its schema and a function, register your resources and prompts, and let the SDK dispatch incoming JSON-RPC requests to the right handler. The build itself is ordinary backend work, the kind a senior Python developer does routinely.

There is one gotcha that catches nearly everyone building a stdio server: never write to stdout for logging or debugging. On a stdio transport, stdout carries the JSON-RPC messages, so a stray print statement corrupts the message stream and breaks the server in confusing ways. Send all logging to stderr instead. It is a small rule, but it is the single most common reason a first MCP server mysteriously fails to connect.

Local vs. Remote: stdio or HTTP

Choose stdio when the server runs on the same machine as the client and acts on local resources: reading local files, running local commands, or talking to a database on localhost. It is the simplest to build and run because there is no network, no hosting, and no authentication to stand up, and it is how most developer-tool MCP servers ship.

Choose HTTP when the server needs to be a shared, always-on service: a company-wide MCP server that many people's AI tools connect to, or one that fronts a hosted system. This is closer to running any production web service, which means you own the infrastructure concerns (deployment, scaling, monitoring) and, critically, authentication and authorization, because the server is now reachable over the network.

A reasonable path is to prototype as a stdio server to prove the tools are useful, then promote it to an HTTP service once you need it shared or hosted. The tool and resource handlers are the same either way, so the transport decision is mostly about who needs to reach the server and what operational surface you are willing to run.

Security: The New Attack Surface

Giving an AI the ability to call your tools is exactly as powerful, and as dangerous, as it sounds. Once an agent can invoke tools through MCP, prompt injection stops being a text problem and becomes an action problem: content the model reads (a retrieved document, a tool's output, a web page) can try to instruct the model to call tools it should not. This is why prompt injection remains OWASP's top LLM risk, and MCP connections have widened the surface with risks like tool poisoning and credential theft through tool output.

The defensive posture is least privilege applied to tools. Scope each tool to the narrowest capability that does the job, avoid exposing a broad execute-anything tool when a handful of specific ones would do, and require explicit authorization for actions that change state or touch sensitive data. Allow-listing which tools a given client may call, and validating tool inputs rather than trusting them, closes off the most direct abuse paths.

Treat the output of tools and retrieved resources as untrusted input, not as instructions to obey, and keep humans in the loop for high-consequence actions. The right mental model for a production MCP deployment is less can the agent do it and more what is the agent allowed to do, under what conditions, and can we prove it after the fact, which means logging every tool call with its inputs and results the same way you would log any production LLM system.

When to Build an MCP Server vs. a Plain API

MCP does not replace your REST or internal APIs; it sits in front of them for AI consumption. If the only consumer is your own application code, a normal API is simpler and you do not need MCP at all. The moment you want AI assistants and agents (possibly several different ones) to use a capability, an MCP server is what makes that reusable without a custom integration per client.

The clearest wins are internal tools you want your team's AI assistants to use, data sources you want agents to query safely, and capabilities you want to expose once and reuse across every MCP-aware client. If you are building agents at all, exposing their tools through MCP rather than hard-wiring them into one framework is the choice that keeps your options open as the framework landscape keeps shifting.

If you are weighing whether an MCP server is the right investment for your use case, or scoping an agent that would use one, that is exactly the kind of architecture decision worth getting right early (the remit of a principal AI engineer), because the tools you expose and how tightly you scope them are hard to walk back once agents depend on them.

Frequently Asked Questions

What is an MCP server in simple terms?

An MCP server is a small program that exposes tools, data, and prompts to AI applications through the Model Context Protocol, a common standard often described as USB-C for AI. Instead of building a custom integration between every AI tool and every data source, you build one MCP server per capability, and any MCP-aware client can use it.

What language do I use to build an MCP server?

Official MCP SDKs are available for Python, TypeScript, and other languages, and they handle the JSON-RPC protocol details, so you implement tool, resource, and prompt handlers rather than the wire format. Python and TypeScript are the most common choices because of their SDK maturity and the ecosystems around AI tooling.

What is the difference between stdio and HTTP MCP servers?

A stdio server runs as a local subprocess that the client launches and talks to over standard input and output, which is simplest for local tools with no network or authentication. An HTTP server runs as a remote, shared service reachable over the network, which suits company-wide servers but requires you to handle hosting, scaling, and authentication.

Why is my stdio MCP server not connecting?

The most common cause is writing to stdout. On a stdio transport, standard output carries the JSON-RPC messages, so any print or log statement to stdout corrupts the message stream and breaks the server. Route all logging to stderr instead, which is the single most frequent fix for a first MCP server that fails to connect.

Is MCP a security risk?

MCP itself is a protocol, but exposing tools to an AI widens the attack surface, because prompt injection can attempt to make an agent call tools it should not. The mitigations are least-privilege tool scoping, allow-listing which tools a client can call, validating tool inputs, treating tool output and retrieved content as untrusted, and keeping humans in the loop for high-consequence actions, with full logging of every tool call.

Sources

Further Reading

Building something like this?

I build custom operations software for home service contractors: field ops platforms, AR dashboards, permit pipelines, and local SEO.If you're scoping a project, I can tell you what it would take for your setup in a quick call.