Connect the Server to a Real Host

An MCP server you have only called from a testing tool does nothing for you yet. To put it to work, an AI application has to start it, talk to it and give its tools to the model. This page explains how a host runs a local server as a child process and exchanges messages with it through its standard input and output, why that channel has to stay clean, how you tell a host the command that starts your server, and what happens once the model can see your tools.

How a host talks to a local server

Every program you start from a terminal gets three standard streams from the operating system: standard input (stdin), which it reads from, standard output (stdout), where it writes its normal output, and standard error (stderr), meant for messages about the program itself. A terminal joins these streams to your keyboard and your screen. Another program can join them to itself instead.

The stdio transport of MCP is built on exactly that. The host (the AI application, such as Claude Code, VS Code or Claude Desktop) launches your server as a subprocess, a child program it starts and owns. The client inside the host then writes JSON-RPC messages to the server's stdin, and the server writes its JSON-RPC messages to its stdout, where the client reads them. Each message is one line of JSON: it ends with a newline and may not contain a newline of its own, so the reader knows a message is complete when the line ends.

  • Host (client inside)
  • Server subprocess
  • stdin: requests
  • stdout: responses only
  • stderr: logs

Three consequences follow from this design. The host starts the server itself, so you never start it first and connect afterwards: there is no port to pick and nothing listens on one. The server lives as long as the host keeps it: when the host is done, it closes the server's stdin, waits for it to exit and ends it by force if it lingers. And if you run the server by hand in a terminal, it prints nothing and does not return. That silence is correct, because it is waiting on stdin for a client to speak first; Ctrl-C stops it. A traceback or an immediate exit is the real sign of a problem.

In the official Python SDK (the mcp package, 2.x line), stdio is the default transport. The server class there is MCPServer. In the 1.x line and in many older tutorials it was called FastMCP, which is also the name of a separate framework, so you will meet both names. Calling run() on the server with no argument serves over stdio, and the call does not return until the server stops:

server.py
from mcp.server import MCPServer

mcp = MCPServer("notes")


@mcp.tool()
def add_note(text: str) -> str:
    """Save a short note and confirm it."""
    return f"Saved: {text}"


if __name__ == "__main__":
    mcp.run()  # stdio is the default transport

True or false?

A stdio server has to be started first so that the host can connect to the port it listens on.

False. With stdio the host launches the server itself as a child process and talks to it through its standard streams. No port is involved.

Running a stdio server by hand and seeing no output at all is normal.

True. The server is waiting for a client to write the first message to its stdin. A crash or an instant exit would be the problem.

Standard output belongs to the protocol

Because the client reads stdout one line at a time and treats every line as a JSON-RPC message, the specification is strict: the server must not write anything to stdout that is not a valid MCP message. A single stray line, such as a greeting printed while the program starts, arrives at the client as a message that is not JSON, which the client cannot parse. Some clients, such as the one in the Python SDK, report the error, throw the line away and carry on; others treat the connection as broken and drop it. Some hosts then show the server as present but with no tools in it, which is confusing until you know the cause.

What the client reads from stdout
Notes server starting...
{"jsonrpc": "2.0", "id": 1, "result": {"tools": [{"name": "add_note", ...}]}}

Standard error is the place for everything else. The server may write any logging there (information, debugging, errors), and the host may show it, save it to a log file or ignore it. Text on stderr does not mean the server failed; it is simply the server's own channel for talking about itself.

In Python, print() writes to stdout by default, so the rule for a stdio server is: never print, log instead. The standard logging module writes to stderr, and the SDK's default logging setup does too, flushing each record as it goes:

import logging

from mcp.server import MCPServer

logger = logging.getLogger(__name__)

mcp = MCPServer("notes")


@mcp.tool()
def add_note(text: str) -> str:
    """Save a short note and confirm it."""
    logger.info("Saving a note of %d characters", len(text))
    return f"Saved: {text}"

The 2.x SDK adds a safety net while the server is serving: output flushed to stdout during that time is diverted to stderr. To see where the net stops, it helps to know that Python does not always write printed text out at once. It may hold the text in a buffer in memory and pass it on later, which is called flushing it. The net catches only what is flushed while the server is serving, so it has holes at both ends:

  • Output that reaches stdout before serving begins still lands on the wire: a line echoed by a wrapper script, or a print at import time that is flushed straight away.
  • A print() whose text stays in the buffer until Python empties it at exit lands there too.

Logging avoids all of these cases. Log output never reaches the model either: only the value a tool returns does.

True or false?

A line of text on the server's stderr tells the host that the server has failed.

False. Stderr is the server's logging channel. Hosts should not read anything written there as an error condition.

A message printed to stdout before the server starts serving can make the host drop the connection.

True. The client reads that line as a protocol message, cannot parse it, and some hosts then treat the connection as corrupt.

Local and remote transports

Stdio is one of the two standard MCP transports. A transport only decides how messages are carried from one side to the other; the specification states that the meaning of the messages is identical on every transport. The other standard transport, Streamable HTTP, is for a server that runs as its own independent process, possibly on another machine, and can serve many clients at once. It offers one HTTP address (an endpoint), and the client sends each message to it as an HTTP POST request.

stdioStreamable HTTP
Where the server runsOn the same machine as the hostAnywhere the host can reach over a network
Who starts itThe host, as its child processYou, before any client connects
ClientsOne: the host that launched itMany at once
What you give the hostA command that starts itA URL
Tools, resources, promptsAll of themThe same ones

With the Python SDK, the server code stays the same and only the argument to run() changes. mcp.run(transport="streamable-http") serves the same tools, resources and prompts over HTTP, by default at http://127.0.0.1:8000/mcp. A local server suits a tool for yourself on your own machine. To share a server with people who do not have your files, you hand out a URL rather than a command. You may also see an older transport called HTTP+SSE; it is deprecated, and Streamable HTTP replaced it.

True or false?

Moving a server from stdio to Streamable HTTP means rewriting its tools for the new transport.

False. The transport only carries the messages. The same server object offers the same tools, resources and prompts on both.

Registering a server with a host

Since the host starts a local server itself, connecting one comes down to telling the host how. Every host asks for the same four pieces: a name for the server, the command to run, its arguments, and the environment variables the server needs.

For the command, the Python SDK recommends one that works from any folder, with no virtual environment to activate. It uses uv, a tool that runs Python programs: uv installs the SDK on the fly and runs the SDK's mcp run command on your file. Here is that command registered with Claude Code:

Claude Code: register a server for yourself
$ claude mcp add --env NOTES_DB=/home/ana/notes/notes.db --transport stdio notes -- uv run --with "mcp[cli]" mcp run /home/ana/notes/server.py
$ claude mcp list

The four pieces are all on that line: notes is the name, --env sets a variable, and everything after -- is the launch command with its arguments, passed on untouched. The host's own options come before the --. The second command lists the servers Claude Code knows about.

The same four pieces appear in a configuration file. A project can keep one in the repository, so everyone who clones the project gets the server. Claude Code reads such a shared file from .mcp.json at the project root:

.mcp.json
{
  "mcpServers": {
    "notes": {
      "type": "stdio",
      "command": "uv",
      "args": ["run", "--with", "mcp[cli]", "mcp", "run", "/home/ana/notes/server.py"],
      "env": { "NOTES_DB": "/home/ana/notes/notes.db" }
    }
  }
}

Hosts differ in where the entry lives, but not in what it says. Claude Desktop keeps a mcpServers object in claude_desktop_config.json, opened from its Developer settings with Edit Config. Current VS Code reads a shared .mcp.json at the workspace root or a personal ~/.copilot/mcp-config.json, both with the same mcpServers key, and its MCP: Add Server command walks you through the entry. Older tutorials show .vscode/mcp.json with a servers key; VS Code has deprecated that format.

Scopes in Claude Code
ScopeWho gets the serverWhere it is stored
local (the default)Only you, in this project~/.claude.json, under this project
projectEveryone who clones the project.mcp.json at the project root, committed
userOnly you, in all your projects~/.claude.json, top-level mcpServers

Three details cause most failed connections:

  • Paths. The host does not launch the server from your terminal: it uses its own working folder and a nearly empty environment with a minimal PATH. A relative path such as server.py is the most common failure, so give absolute paths. If the host cannot find uv, use the full path that which uv (macOS, Linux) or where uv (Windows) prints.
  • Variables. Every variable the server needs has to be in env, because your shell's variables are not passed along.
  • Restarting. Hosts read their configuration when they start, so restart the host after an edit. Claude Desktop has to be quit completely, not only its window closed.

A shared project file starts programs on your machine, so hosts ask before trusting one: Claude Code asks for approval before it uses servers from a project's .mcp.json, and VS Code ties them to Workspace Trust.

True or false?

A host starts a local server in the folder of the terminal you were using, with your shell's variables.

False. The host uses its own working folder and a near-empty environment. Absolute paths and an explicit env avoid both problems.

A server registered in a committed .mcp.json file is available to everyone who clones the project.

True. That is the purpose of the project scope. Each person still approves the server before the host uses it.

Checking the launch command with a client

A host is an application with an MCP client inside it, so a short Python script can play its part. Giving the client the same command, arguments and environment as the host entry starts the server exactly the way the host will: as a subprocess, over stdio. If the script lists the expected tools, the launch command is right before any model is involved. If it fails, the error appears in your terminal, together with the server's stderr, which the client passes through by default.

check_launch.py
import asyncio

from mcp import Client, StdioServerParameters

server = StdioServerParameters(
    command="uv",
    args=["run", "--with", "mcp[cli]", "mcp", "run", "/home/ana/notes/server.py"],
    env={"NOTES_DB": "/home/ana/notes/notes.db"},
)


async def main() -> None:
    async with Client(server) as client:
        result = await client.list_tools()
        print([tool.name for tool in result.tools])


asyncio.run(main())

The print() here is fine: this script is the client, and its own stdout is your terminal, not a protocol channel. The client also behaves like a host in one more way that matters: the child process receives only a short allow-list of environment variables plus whatever you put in env. A server that works here and fails in the host usually points to a path or a variable that differs between the two entries. The MCP Inspector does the same job interactively, since it also launches the server as a subprocess and connects over stdio.

The model in the loop

Once the server is connected, the model never talks to it directly. The host's client asks the server which tools it has, and the host hands that listing (each tool's name, description and input schema) to the model along with the conversation. When the model decides a tool would help, it answers with a request to call one, with arguments. The host shows that request to the user, and only after the user approves does the client send the call to the server. The result goes back to the model, which uses it to continue its answer.

  • User
  • Host and client
  • Model
  • Server
  • Host and client to Server: tools/list
  • Server to Host and client: tool list
  • User to Host and client: a question
  • Host and client to Model: question + tools
  • Model to Host and client: call add_note
  • Host and client to User: allow this call?
  • User to Host and client: allow
  • Host and client to Server: tools/call
  • Server to Host and client: result
  • Host and client to Model: result

The approval step comes from the specification, which says there should always be a human in the loop who can deny a tool call, and that applications should show which tools the model can use and ask the user to confirm operations. Hosts implement it in their own way: Claude Code asks for permission the first time a tool runs and lets you save the choice, Claude Desktop asks for your approval before an action runs, and VS Code offers to allow a call once or to remember the approval. Whatever the host, the model only chooses from what the listing describes, so the names, descriptions and schemas you wrote are all it has to go on.

True or false?

The model connects to the MCP server and calls its tools directly.

False. The host's client holds the connection. The model only sees the listing and asks the host for a call, which the host can put to the user first.

A user can refuse a tool call that the model has chosen.

True. The specification asks hosts to keep a human able to deny tool calls, and hosts show a confirmation before a call runs.