A small demo showing a minimal Model Context Protocol (MCP) server and a client that connects to it over stdio.
This repository contains a toy calculator server exposing a few tools (add, subtract, multiply, divide) and a small client that demonstrates how to list resources/tools, read a resource, and call a tool.
client/calc_client.py— example async client that runs a server process and communicates over stdio.
server/calc_server.py— example MCP server that registers calculator tools and a greeting resource.
README.md— this file.
- Python 3.8+ (the environment where you run the client and server should have a compatible Python version). The code was exercised with Python 3.13 but any modern 3.x should work.
- The
mcppackage (the MCP framework used by the examples) must be importable in the Python environment. That means either:- you have
mcpinstalled into the active virtualenv, or - your PYTHONPATH includes the location of an
mcppackage/source tree.
- you have
If you don't have mcp available, install it through your normal package source or make the package importable via PYTHONPATH.
Open PowerShell and change into this repository's py_mcp directory:
cd C:\path\to\py_mcpOption A — run with an environment that already has mcp installed:
# Run the server in one terminal
python .\server\calc_server.py
# In another terminal (same virtualenv/interpreter) run the client
python .\client\calc_client.pyOption B — if mcp is a local package sibling (or you want to run without installing), add the current directory to PYTHONPATH so mcp can be imported:
$env:PYTHONPATH = $PWD
python .\server\calc_server.py
# In another terminal (same $env:PYTHONPATH value):
$env:PYTHONPATH = $PWD
python .\client\calc_client.pyIf you'd like an interactive visual inspector to explore the running MCP server, you can use the Model Context Protocol Inspector. It launches a local UI that connects to a server process and displays registered tools, resources, and messages exchanged over the protocol.
Run this command from the server directory (it uses npx to run the inspector without installing globally):
npx @modelcontextprotocol/inspector python calc_server.pyNotes about the inspector:
- The inspector will spawn the server script using the Python interpreter you provide (here
python calc_server.py). Make sure the same interpreter has access to themcppackage. - The inspector is a developer tool for inspecting and debugging MCP servers — it shows available tools/resources, request/response messages, and helps you exercise tools interactively.
- If the command fails with errors about
npxor permissions, ensure Node.js (which providesnpx) is installed and available in your PATH, or install the inspector package globally or locally and run it directly.
Inspector troubleshooting
Common issues you may see when running the inspector and how to resolve them:
- "'npx' is not recognized as an internal or external command" — Node.js / npm is not installed or not on your PATH. Install Node.js (which includes npm) from https://nodejs.org/ and re-open your terminal.
- "Permission denied" or EACCES when running
npx— on Windows this is uncommon; try running PowerShell as Administrator, or install the inspector package locally in the project (npm i @modelcontextprotocol/inspector) and run it vianpxornpx --no-install @modelcontextprotocol/inspector .... ModuleNotFoundError: No module named 'mcp'— the inspector spawns your server using the Python interpreter you provided. Ensure that interpreter has access to themcppackage (install it into the venv, or setPYTHONPATHto include the package location). You can reproduce by running the server script directly:
python .\calc_server.pyand fixing any import errors there first.
- Inspector fails to connect / shows transport errors — verify the server script is being launched by the inspector and that it speaks MCP over stdio. The inspector expects a server process that adheres to the Model Context Protocol over stdio when launched this way.
Notes:
- The example client uses
sys.executableto launch the server, so use the same Python interpreter / virtualenv for both server and client to avoid import issues. - If you see ModuleNotFoundError: No module named 'mcp', verify that
mcpis installed or that PYTHONPATH points to a folder containing anmcppackage.
server/calc_server.pyregisters four tools:add,subtract,multiply, anddivide. It also exposes a dynamic resource with the templategreeting://{name}that returns a greeting string for the name provided.client/calc_client.pylaunches the server as a subprocess (stdio transport), initializes an MCPClientSession, lists resources and tools, reads thegreeting://helloresource, and calls theaddtool witha=1, b=7.
Example (trimmed) output you should see when running the client:
Using server script at: C:\...\py_mcp\server\calc_server.py
LISTING RESOURCES
Resource: greeting://{name}
LISTING RESOURCE TEMPLATES
Template: greeting://{name}
LISTING TOOLS
Tool: add
Tool: subtract
Tool: multiply
Tool: divide
READING RESOURCE
Content: Hello, hello!, MIME Type: text/plain
CALL TOOL
1 + 7 = 8 # or similar output depending on how the client prints the tool result
- Inputs: client requests contain resource identifiers (string templates) or tool names with JSON-like argument dictionaries.
- Outputs: resource reads return (content, mime_type); tool calls return a result object (the demo prints the tool result).
- Error modes: server returns exceptions for invalid tool calls; division by zero in
dividereturnsinfin the example server implementation.
- Division by zero:
divide(a, 0)returnsfloat('inf')in the server implementation — adjust if you prefer an exception or custom error object. - Keep server and client running with the same Python interpreter to avoid import / path mismatches.
- If your environment blocks subprocess stdio connections (uncommon), run server and client in separate terminals and adapt the client to connect over a different transport.
- ModuleNotFoundError: No module named 'mcp' — ensure
mcpis installed or reachable via PYTHONPATH. - Permission / PATH issues launching Python — use the full path to the python executable you want to use (or run from the desired venv's prompt).
- Add a
requirements.txtor pyproject configuration if this project should declare dependencies. - Add unit tests that exercise the client-server interaction (using a fake stdio transport or subprocess with captured I/O).
- Extend the server with more tools and resource types to explore the MCP features.
No license specified. Add a LICENSE file if you want to make the project's license explicit.