Your First MCP Server: Python, Claude Desktop and the Pitfalls That Block Connections
Build a small Python MCP server, connect it to Claude Desktop, diagnose stdio and configuration errors, and explore filesystem, GitHub, fetch, Postgres and search integrations.
Contents
A server can run successfully and still fail to connect to Claude Desktop. One easy mistake explains many confusing stdio failures: writing diagnostic output to standard output. The client expects JSON-RPC messages there, so ordinary print() output corrupts the protocol stream.
This guide starts with a small server, then works through configuration, diagnostics and useful integrations.
MCP is a protocol
MCP, the Model Context Protocol, specifies how a client and server exchange messages. It is different from a framework abstraction or a particular provider's function-calling API.
You can implement a server in Python, TypeScript, Rust, Go or another suitable language, and connect it to compatible clients such as Claude Desktop, Cursor or Continue. A framework may use MCP, but comparing MCP directly with an orchestration framework misses the distinction between their roles.
Three concepts in the protocol
Resources expose information for a client to read, referenced through URIs. Files, database content and API results can be presented this way.
Tools expose callable operations. Their descriptions and JSON Schema inputs tell the client what they do and which arguments they accept. Tool calls can include operations such as create_pull_request(...) or run_sql_query(...).
Prompts are reusable prompt templates supplied by a server. You can understand the concept without implementing prompts in your first server.
Distinguish reading data from executing an action. Tool permissions and side effects need explicit design; resource access also needs appropriate limits on which information is available.
Build a small Python server
Install the packages:
pip install fastmcp httpx
FastMCP provides a higher-level interface for defining tools. This server retrieves public repositories for a GitHub user:
from fastmcp import FastMCP
import httpx
mcp = FastMCP("github-tools")
@mcp.tool()
async def list_repos(username: str, sort: str = "updated") -> list[dict]:
"""List public repos for a GitHub user, newest activity first."""
async with httpx.AsyncClient() as client:
r = await client.get(
f"https://api.github.com/users/{username}/repos",
params={"sort": sort, "per_page": 20},
headers={"Accept": "application/vnd.github.v3+json"}
)
r.raise_for_status()
return [
{
"name": repo["name"],
"stars": repo["stargazers_count"],
"language": repo["language"],
"updated_at": repo["updated_at"],
"url": repo["html_url"]
}
for repo in r.json()
]
if __name__ == "__main__":
mcp.run()
The @mcp.tool() decorator registers the function. Its docstring supplies the description and its type annotations describe inputs. The server uses stdio in this workflow.
Run it:
python github_server.py
A process that waits silently is expected: the server is waiting for client messages.
Connect it to Claude Desktop
The configuration paths used by this workflow are:
| OS | Configuration path |
|---|---|
| macOS | ~/Library/Application Support/Claude/claude_desktop_config.json |
| Windows | %APPDATA%\Claude\claude_desktop_config.json |
| Linux | ~/.config/Claude/claude_desktop_config.json |
Create the configuration file if needed and add the server:
{
"mcpServers": {
"github-tools": {
"command": "python",
"args": ["/Users/you/path/to/github_server.py"]
}
}
}
Use absolute paths. In a virtual environment, use the full path to its Python interpreter rather than assuming the desktop app inherits your shell's PATH. Do not rely on shell expansion of ~.
Save the file and fully quit the application before restarting it. Closing the window alone may leave the old process and configuration active. Find the server in the client's integrations or tools, then ask for the latest five public repositories from anthropics.
Three connection pitfalls
1. Standard-output pollution. Keep stdout reserved for protocol messages. Send diagnostics to stderr:
import sys
print("debugging info", file=sys.stderr)
# Or configure logging with a stderr handler
A startup message printed to stdout can be enough to break parsing.
2. Looking in the wrong place for errors. Inspect the client logs as well as the server process. The paths documented for this workflow are:
| OS | Log path |
|---|---|
| macOS | ~/Library/Logs/Claude/mcp*.log |
| Windows | %LOCALAPPDATA%\Claude\Logs\mcp*.log |
| Linux | ~/.local/share/Claude/logs/mcp*.log |
Missing dependencies, invalid configuration and interpreter paths should be investigated in the relevant server log.
3. Connecting the desktop client before testing the server. First try MCP Inspector:
npx @modelcontextprotocol/inspector python github_server.py
Its web interface lets you invoke a tool and inspect its result, separating server problems from desktop configuration problems.
Five integrations to understand
The MCP servers repository is an entry point for reference implementations and related projects. Check a server's current maintainer, package and documentation before installation. The configurations below preserve the package-based workflows described in this guide.
Filesystem
Expose one directory containing files you intend the assistant to read:
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/Users/you/Documents/notes"]
}
A focused notes directory can support questions such as “What did I write about RAG last week?” The assistant can retrieve relevant notes without depending on an editor's codebase context.
Do not expose your entire home directory. Hidden files within an allowed directory can include .env files, SSH keys and cloud credentials. Prepare a clean directory and allow only the material needed for the task; do not assume hidden files will be ignored.
GitHub
The package configuration in this workflow uses a dedicated GitHub token:
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": { "GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_xxxxx" }
}
One useful task is reviewing recently merged pull requests and preparing a one-line changelog for each, using list and detail tools.
Grant the minimum permissions required by the chosen server and repository scope. Use a dedicated credential rather than reusing a broad personal token, and keep real tokens out of source control.
Fetch
The Python fetch server can run with uvx:
"fetch": {
"command": "uvx",
"args": ["mcp-server-fetch"]
}
It can retrieve documentation and release notes for the assistant to read. Fetching HTML does not imply browser JavaScript rendering: single-page applications may return little content, and protected pages may reject requests. A search integration can help locate a suitable source before fetching it.
Postgres
Use a local copy or a read replica with a read-only database account. Confirm the permissions of the particular implementation you install; do not assume every community server enforces the same limits.
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres", "postgresql://user:pass@localhost/dbname_replica"]
}
For data exploration, ask a question such as “How many users created last week have verified their email?” Review generated SQL and results. Keep the connection away from a production write account.
Brave Search
A search integration can locate sources before the fetch server retrieves details. It requires the provider's API credential, commonly configured as BRAVE_API_KEY. Check the current service terms and account allowance rather than assuming a particular free quota.
A workflow across several servers
Combine a filesystem server, GitHub integration, fetch and search to prepare a weekly reading digest:
- Find notes marked unread in the allowed notes directory.
- Retrieve updates from selected GitHub projects.
- Locate and fetch the relevant release notes.
- Produce a short digest with links back to the sources.
Define the allowed repositories, directories and external actions in advance. Multiple integrations are useful when each contributes a specific part of the task.
Turn the server into a project
The JR Academy AI Engineer learning path includes MCP alongside LLM applications, RAG, agents and deployment. A working server that you can explain and demonstrate gives you concrete material for a portfolio discussion.
If you need programming foundations first, follow the Python learning path. For managing the information supplied to agents, explore the Context Engineering topic.
A remote deployment introduces further decisions around Streamable HTTP, authentication and operations. Get the stdio workflow working first, then design the remote access boundary deliberately.
Continue learning
- AI Engineer learning path: LLM applications, RAG, agents, MCP and Context Engineering.
- Context Engineering: A ten-chapter bilingual topic.
- Bootcamp information.
Related certifications
Start with a few sample questions or a free chapter. The exam guide covers what the exam tests; the question bank lets you practise by exam domain.
Claude Certified Architect – Foundations
CCAR-F
Claude Certified Developer – Foundations
CCDV-F
Claude Certified Associate – Foundations
CCAO-F