MCP Server Tutorial: Build One That Actually Connects
A step-by-step MCP server tutorial: pick a transport, write the smallest correct server in TypeScript or Python, wire it into Claude Desktop or Cursor, and debug the three failures that account for most silent no-shows.
What you are actually building
An MCP server is a process that answers three kinds of questions from a client: what tools do you have, what arguments does each one take, and here is a call, run it. That is the whole contract. Everything else — resources, prompts, sampling, notifications — is optional surface area you can ignore until the first tool works.
The part that surprises people is who starts the process. In the stdio transport, the client does. Claude Desktop or Cursor reads a config entry, spawns your binary as a child process, and talks to it over stdin and stdout. You never run it yourself, you never expose a port, and there is no request that arrives from the network. This is why a server that works perfectly in a terminal can still fail to appear in the app: the app is not calling the same command you tested.
Before writing anything, confirm what version of the SDK you are about to use. The snippets below follow the high-level server API that exports a McpServer class and a server.tool() registration method. If your installed version renamed either of those, the README on the package page is the authority, not this article.
npm view @modelcontextprotocol/sdk version
pip show mcp | head -2Choose the transport first
Transport decides almost everything downstream: how you debug, how you ship, and what breaks. Two matter for a first server.
| stdio | The client launches your process and speaks JSON-RPC over stdin/stdout. No port, no auth, dies with the client. Best for local tools and the fastest path to a working handshake. |
|---|---|
| HTTP | Your server listens on a URL and the client connects to it. Needed for anything shared, remote, or long-running. Adds auth, deployment, and a whole class of timeout bugs. |
Start with stdio even if you intend to ship HTTP later. The tool definitions and handlers are the same code; only the transport wrapper changes. Getting the handshake working locally removes the network from your list of suspects, which is worth more than the hour it costs to refactor later.
TypeScript: the smallest correct server
Two things make a first server fail: forgetting that stdout is the protocol channel, and returning a shape the client cannot render. Any console.log you leave in for debugging will corrupt the stream, because the client parses every line on stdout as JSON-RPC. Log to stderr instead.
// package.json: "type": "module"
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const server = new McpServer({ name: "demo", version: "0.1.0" });
server.tool(
"count_words",
"Count words in a block of text and report the longest word.",
{ text: z.string().describe("The text to analyse") },
async ({ text }) => {
const words = text.trim().split(/\s+/).filter(Boolean);
const longest = words.reduce((a, b) => (b.length > a.length ? b : a), "");
return {
content: [
{
type: "text",
text: `${words.length} words; longest is "${longest}"`,
},
],
};
}
);
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("demo server ready on stdio");The handler returns an array of content blocks, not a bare string. That shape is what lets a server return text, an image, or a reference to a resource from the same tool. Returning a plain string works in some clients and silently renders nothing in others, which is the kind of bug that costs an evening.
Build it and check the output path before touching any config file. Print the absolute path to the built entry point — every config mistake below is a path mistake wearing a different hat.
npm install @modelcontextprotocol/sdk zod
npx tsc && node -e "console.log(require('path').resolve('build/index.js'))"Python: the same server, shorter
The Python SDK ships a decorator layer that removes most of the boilerplate. The same caution applies: nothing may print to stdout.
# pip install mcp
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("demo")
@mcp.tool()
def count_words(text: str) -> str:
"""Count words in a block of text and report the longest word."""
words = text.split()
longest = max(words, key=len) if words else ""
return f'{len(words)} words; longest is "{longest}"'
if __name__ == "__main__":
mcp.run()If the import line fails, the package name and the import path differ across versions — check the installed distribution rather than guessing. The tool decorator derives the schema from the type hints, so a stub with no annotations produces a tool the model cannot fill in correctly.
Wiring it into Claude Desktop or Cursor
Both clients use the same JSON shape and differ mainly in where the file lives. command plus args is exactly what they will execute, so both must be absolute — a relative path resolves against the client's working directory, not yours.
{
"mcpServers": {
"demo": {
"command": "node",
"args": ["/absolute/path/to/build/index.js"]
}
}
}Claude Desktop reads claude_desktop_config.json from its application support directory, which differs per OS; Cursor reads .cursor/mcp.json in the project or a global equivalent. After editing, fully quit and reopen the app — several versions only read the config at launch, so a reload is not enough and the entry will look like it was ignored.
Do not create a new server, if you have not read what an MCP server is yet; the host-client-server split explains why the app, not your terminal, owns the process. And if you want a working example of a server that drives real tools rather than toy functions, REA is worth reading as a case study.
Adding a second tool without breaking the first
The failure mode here is not code, it is selection. Once two tools exist, the model chooses between them, and it chooses from the description text. Two tools described as "search the codebase" and "look things up in the repo" will be used interchangeably, and half your calls will go to the wrong handler.
Write each description as the sentence you would say when handing the task to a new colleague, including the boundary. "Count words in a block of text — use this for strings already in the conversation, not for files on disk" is a description that prevents a whole category of wrong calls. The description is not documentation, it is the routing decision.
Three silent failures and how to see them
A broken MCP server usually does not error. It just does not appear, or appears with no tools. Three causes account for most of that.
| Nothing in stdout but JSON-RPC | Any stray print, banner, or warning on stdout makes every subsequent line unparseable. Move all logging to stderr and test again. |
|---|---|
| Absolute path required | The client spawns from its own directory. A path that works in your shell will fail there. Resolve it and paste the resolved value. |
| Process exits immediately | Usually a missing dependency or an await at top level in a CommonJS build. Run the exact command from the config by hand and read stderr. |
The inspector removes the guesswork: it connects to your server the way a client would and lists the tools it discovers. If the inspector sees the tools and the app does not, the problem is in the config, not in the server.
npx @modelcontextprotocol/inspector node /absolute/path/to/build/index.jsIf you need C#
There is an official C# SDK, and it is a reasonable choice if the tool you are wrapping already lives in .NET. It is a poor choice for a first server, because the tutorials assume a working project setup and the error surface is larger. The protocol is the same, so anything you learn from the TypeScript version transfers: three methods, one transport, stdout is sacred.
FAQ
Do I need a server, a database, or any infrastructure?
How many tools should the first version expose?
Why does my server not appear in Claude Desktop?
TypeScript or Python?
Can I write it in C#?
Is an MCP server the same thing as an API?
Related: What is an MCP server · How to use MCP servers with Claude · MCP servers worth connecting
Learn how MCP servers work →See a real MCP server driving Ghidra and IDA →how do AI agents work — return to the complete AI agent architecture guide.
Was this helpful?
Your feedback stays on this page — no tracking.