Sub topic 12
Connecting a host application
This chapter assumes you have a working server from the earlier chapters (the team-tools server with get_service_owner), that you tested it with the MCP Inspector, and that you know what stdio and Streamable HTTP are. Here you wire the server into a host application so a model can call it.
What the host does with your configuration
A host is the application the user talks to, such as Claude Desktop, an IDE, or a CLI agent. The host does not import your code. It reads a configuration file, starts or contacts each server listed there, and creates one MCP client per server. Each client then runs the initialization handshake and asks for the server's tools, resources, and prompts.
The configuration answers one question: how do I reach this server? For a local stdio server, the answer is a command to run. For a remote server, the answer is a URL.
Configuring a local stdio server
Many hosts, including Claude Desktop, use a JSON file with an mcpServers object. Each key is a name you choose, and each value says how to launch the server.
{
"mcpServers": {
"team-tools": {
"command": "python",
"args": ["/absolute/path/to/server.py"]
}
}
}The fields mean the following:
team-toolsis the display name. The host may use it to prefix or group your tools, so pick a short, descriptive one.commandis the executable the host starts.argsis the list of arguments passed to it. Each argument is a separate string. Do not put a whole command line in one string.
The file name and location differ between hosts, and some use different keys. Check your host's documentation for the exact format. The structure above is common but not universal.
Use absolute paths
The host starts your server from its own working directory, which is often not your project folder. A relative path such as server.py usually fails with "file not found". Use an absolute path to the script.
The same problem applies to python. The host may find a different interpreter from the one in your terminal, and that interpreter may not have the mcp package installed. Point command at the exact interpreter in your virtual environment:
{
"mcpServers": {
"team-tools": {
"command": "/home/dana/projects/team-tools/.venv/bin/python",
"args": ["/home/dana/projects/team-tools/server.py"]
}
}
}If you use uv, you can let it manage the environment instead. The --directory option sets the project folder, so relative paths inside the project work:
{
"mcpServers": {
"team-tools": {
"command": "uv",
"args": ["--directory", "/home/dana/projects/team-tools", "run", "server.py"]
}
}
}The uv binary must be on the host's PATH. GUI applications often start with a shorter PATH than your shell. If the host reports that it cannot find uv, put the full path to the binary in command.
Passing environment variables
Team tools usually need credentials or endpoints, such as an API token for your deploy system. Most hosts support an env object in the server entry:
{
"mcpServers": {
"team-tools": {
"command": "/home/dana/projects/team-tools/.venv/bin/python",
"args": ["/home/dana/projects/team-tools/server.py"],
"env": {
"DEPLOY_API_URL": "https://deploy.internal.example.com",
"DEPLOY_API_TOKEN": "replace-me"
}
}
}
}Your server reads these with os.environ:
import os
DEPLOY_API_URL = os.environ["DEPLOY_API_URL"]
DEPLOY_API_TOKEN = os.environ["DEPLOY_API_TOKEN"]Many hosts give a stdio server only a small, fixed set of inherited environment variables. Do not assume it sees everything from your shell. Declare each variable you need in env.
Connecting to a remote server
If you run the server over Streamable HTTP, the host connects to a URL instead of launching a process. Support and syntax vary more here. Some hosts accept a url field in the same file:
{
"mcpServers": {
"team-tools": {
"url": "https://mcp.internal.example.com/mcp"
}
}
}Other hosts use a different key, require a type field such as "http", or add remote servers through a settings screen. Remote servers also need authentication. The host may send a bearer token in a header or run an OAuth flow, depending on what the host and server support. The security chapter covers this. For now, remember the practical difference: with stdio the host owns the server's lifetime, and with HTTP it does not.
| Aspect | Local stdio | Remote Streamable HTTP |
|---|---|---|
| Config points to | A command and arguments | A URL |
| Who starts the server | The host | You or your infrastructure |
| Credentials come from | The `env` block | Headers or an auth flow |
| One server per | User machine | Whole team |
| Update by | Each user pulling new code | Redeploying once |
Verifying the connection
After editing the configuration, restart the host. Most hosts read the file only at startup. Then check three things:
- The server appears in the host's list of connected servers, with no error status.
- The tools you expect are listed, with the descriptions you wrote.
- A real request works. Ask the model, "Who owns the billing service?" and confirm it calls
get_service_ownerand answerspayments-team.
Most hosts ask the user to approve a tool call the first time. That is expected, and it is a feature. It is one of the confirmation points the security chapter relies on.
Troubleshooting a server that will not connect
Because the host starts your process, failures often appear as a vague "server disconnected" message. Work through these causes in order.
- The command does not run. Copy the exact
commandandargsinto a terminal and run them. If the process exits at once, fix that first. A healthy stdio server runs and waits for input. - Wrong interpreter or missing package. An
ImportErrorformcpmeans the host used a Python without the SDK. Use the full interpreter path from your virtual environment. - Relative paths. Replace each with an absolute path, or use
--directorywithuv. - Output on stdout. With stdio, stdout carries protocol messages. A stray
print()adds text the client cannot parse as JSON-RPC, and the connection breaks. Send logs to stderr, as shown earlier:
import logging
import sys
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
logger = logging.getLogger("team-tools")
logger.info("team-tools server starting")- Missing environment variables. A
KeyErrorat startup usually means the host did not pass a variable. Add it toenv. - Stale configuration. The host has not been restarted since the edit.
Hosts write server stderr to log files. The location depends on the host, so look in its documentation. Reading that log is the fastest way to see a Python traceback.
You can also reproduce the host's launch with the Inspector. Run it with the same command and environment variables, then confirm the tools list loads:
DEPLOY_API_URL=https://deploy.internal.example.com \
DEPLOY_API_TOKEN=replace-me \
npx @modelcontextprotocol/inspector /home/dana/projects/team-tools/.venv/bin/python /home/dana/projects/team-tools/server.pyIf this works but the host fails, the difference is in the environment the host provides: PATH, working directory, or inherited variables.
Edge cases to plan for
- Many servers at once. A host can load several servers. If two servers expose a tool with the same name, behavior depends on the host. Some prefix names with the server name and some do not. Use specific tool names such as
get_service_owner, notget. - Too many tools. Every tool's name, description, and schema is sent to the model on each request. A large set uses context and makes tool selection less accurate. Connect only the servers a task needs.
- Changes while running. If you add a tool, the host often does not notice until it restarts or reconnects. Servers can announce changes through list-changed notifications, but only if the host supports them.
- Per-project configuration. Some hosts allow a project-level file that can be shared in a repository. That suits non-secret settings. Keep tokens out of it and use per-user configuration or environment references for secrets.
Exercise
Your server works in the MCP Inspector, but the host shows "server disconnected" right after launch. The config uses "command": "python" and "args": ["server.py"]. Name two likely causes and the fix for each.
Show answer
First, the path is relative, and the host's working directory is not the project folder, so use the absolute path to server.py. Second, python may resolve to a different interpreter than your virtual environment's, one without the mcp package, so set command to the full path of the virtual environment's Python (or use uv --directory ... run). Another valid cause is a print() writing to stdout, which breaks the stdio stream. Send logs to stderr instead.
Exercise
Why should you declare DEPLOY_API_TOKEN in the env block instead of assuming the server inherits it from your shell, and what should you do to keep that file safe?
Show answer
Hosts often start stdio servers with a limited set of inherited environment variables, so a variable set in your shell may be missing and cause a startup error. Declaring it in env makes it explicit. Because the file then holds a secret, keep it out of shared repositories, prefer a secret store or variable reference if the host supports one, and limit the token's permissions to what the tools need.