How to Build an MCP Server: Tools, Resources and Prompts From Scratch

Build an MCP server from scratch: official Python and TypeScript SDKs, tools, resources and prompts, the stdout rule that breaks beginners, and safe remote use.

By Galactic News Editorial · Published 2026-11-16T07:33:33+00:00 · Updated 2026-11-16T07:33:33+00:00

How to build an MCP server with tools resources and prompts

How to Build an MCP Server: Tools, Resources and Prompts From Scratch

Learning how to build an MCP server takes less than an hour. The Model Context Protocol is an open standard that lets a language model call your code, read your data, and use your prompts. You define three kinds of capability, pick a transport, and connect a host. This tutorial builds a small weather server in Python, then shows the same ideas in TypeScript. No model key is needed for the server itself.

What an MCP Server Is

MCP is an open protocol introduced by Anthropic. The specification is published at modelcontextprotocol.io. It solves one problem: every AI app used to need its own custom integration for every data source. MCP gives them a shared contract, so one server works in Claude Desktop, Claude Code, Cursor, VS Code, and other hosts.

A server exposes three primitives. Tools are functions the model can call, with user approval. Resources are file-like data the client can read, such as API responses or documents. Prompts are pre-written templates that help users accomplish a task. Most servers start with tools and add the other two later.

Two transports carry the messages. STDIO runs the server as a child process of the client and talks over standard input and output. It is the simplest choice for a local tool. Streamable HTTP runs the server as a network service that many clients can reach. It superseded the older SSE transport for production use. Start local with STDIO. Move to HTTP only when other people need the server.

How to Build an MCP Server in Python: The Steps

The official quickstart builds a weather server with two tools: get_alerts and get_forecast. We follow the same shape here, because it is the smallest server that shows every concept. The steps below use the official Python SDK.

  1. Install Python 3.10 or higher. The SDK requires it.
  2. Install the official SDK with pip install mcp, or use uv for project management. Pin the version in your project file.
  3. Create a file called weather.py and define the server with the SDK's high-level server class.
  4. Define each tool with a function, a name, and a description. Type hints and docstrings generate the tool schema automatically.
  5. Add a resource that exposes a simple data feed, such as a service catalogue.
  6. Add a prompt template for a common task, such as a weather briefing.
  7. Test the server with MCP Inspector, the official debugging client.
  8. Connect it to a host by adding an entry under the mcpServers key in the host config.

The official tutorial uses the National Weather Service API as the data source. Any read-only API works the same way. Pick one you already use, so the server is useful on day one.

The Code: A Minimal Python Server

The Python SDK ships a high-level server interface. In the 1.x line it is called FastMCP. In the 2.x line, released with the July 2026 protocol revision, the class was renamed MCPServer. Pin one version and use its documented import. The shape is the same either way.

from mcp.server import MCPServer

mcp = MCPServer("weather")
NWS_API_BASE = "https://api.weather.gov"

@mcp.tool()
async def get_alerts(state: str) -> str:
    """Get active weather alerts for a US state."""
    ...

@mcp.tool()
async def get_forecast(latitude: float, longitude: float) -> str:
    """Get the weather forecast for a location."""
    ...

Each tool is a plain function with a decorator. The docstring becomes the description the model sees. The type hints become the input schema. Keep descriptions concrete. A model picks tools by their descriptions, so write what the tool does and what it returns.

Run the server with uv run weather.py. It listens for JSON-RPC messages on standard input and output. That is the whole server. There is no web framework, no routing, and no deployment step for a local server.

The One Rule That Breaks Every Beginner Server

Never write to stdout in a STDIO server. The protocol sends JSON-RPC messages over standard output. A stray print statement, a noisy library, or a debug log will interleave with the protocol stream and corrupt the frames. The server then fails with a confusing error.

Send every log line to stderr instead. Python's logging module defaults to stderr. If you reach for print, use file=sys.stderr. For HTTP-based servers, standard output logging is fine, because it does not interfere with HTTP responses. This single rule saves more debugging time than any other in MCP work.

Adding Resources and Prompts

Tools do things. Resources expose data. A resource has a URI, such as weather://alerts/CA, and returns content when the client reads it. Use resources for data the model should read without side effects: a service catalogue, a runbook, a reference table. The client decides when to read them.

Prompts are templates with arguments. A prompt called weather-briefing might take a city and return a pre-written instruction that tells the model how to present the forecast. Prompts save users from typing the same long instruction every time. They are the least used primitive, and the easiest to add later.

Keep the three primitives separate in your head. A tool executes code and may cause a side effect, like a POST. A resource reads data, like a GET. A prompt is a saved instruction. If you are unsure which one a capability is, ask whether it changes anything. If yes, it is a tool.

Testing With MCP Inspector and Connecting a Host

MCP Inspector is the official debugging client, with a web UI, a terminal UI, and a command line mode. Point it at your server and it lists the tools, resources, and prompts. You can call a tool by hand and see the raw JSON-RPC traffic. Test in Inspector before any real host touches the server.

To connect a host, add the server to the host's config file under the mcpServers key. For Claude Desktop, the entry names the command that starts the server and its arguments. The host starts the server as a child process and talks to it over STDIO. Restart the host after editing the config. The server then appears in the host's tool list.

Test the full loop with a real question. Ask the host for the weather alerts in a state and watch it call get_alerts. If the call fails, check three things: the server starts cleanly on its own, nothing writes to stdout, and the tool descriptions are clear.

Going Remote: Streamable HTTP and Security

A local server is safe by default. A remote server is not. The moment the server listens on a network port, anyone who can reach it can call its tools. Add authentication before you expose it. The protocol revision of July 2026 changed the handshake and session handling, so pin the SDK version and read the migration guide for the version you use.

Three rules cover most remote deployments. First, require authentication on every request. An unauthenticated endpoint is an open door. Second, validate every tool input on the server, even if the model usually sends clean data. Third, run the server stateless behind a load balancer when you scale, so any replica can answer any request.

Log tool calls with their arguments and results. A remote server that no one monitors is a liability. Keep the log in a file or a logging service, never on stdout for STDIO servers.

Bottom Line

Building an MCP server is mostly deciding what the model should be able to do. Pick one data feed, expose two or three tools, and test in Inspector. The Python SDK and the TypeScript SDK are both official and both small. Start with STDIO, keep logs on stderr, and add HTTP only when other people need the server. A shipped server is a small, self-contained piece of proof that your system works with any MCP host.