mcp-tool-server
README.md

# mcp-tool-server
**A Model Context Protocol (MCP) server written from scratch in pure Python — no SDK — so the protocol itself stays readable end to end.**



MCP is how AI clients (Claude Desktop, Cursor, agent frameworks) discover
and call external tools through one standard handshake. Most people meet it
through an SDK; this project implements the protocol directly — JSON-RPC
2.0 over stdio, one message per line — in about 150 lines, with four
working tools: a calculator, a word counter, and a small notes store
(add + search). Read it once and you understand what every MCP SDK is
doing on your behalf.
## What it does
- Implements the core MCP lifecycle: `initialize` handshake, `tools/list`, `tools/call`, `ping`
- Exposes tools with JSON Schemas; validates required arguments before running them
- Returns tool failures as tool results (`isError`), never as protocol crashes
- Logs to stderr only — anything else on stdout would corrupt the JSON-RPC stream
## Demo
```console
$ printf '%s\n' \
'{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}' \
'{"jsonrpc":"2.0","method":"notifications/initialized"}' \
'{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"calculator","arguments":{"expression":"12 * (3 + 4)"}}}' \
| python main.py
{"jsonrpc": "2.0", "id": 1, "result": {"protocolVersion": "2024-11-05", "capabilities": {"tools": {}}, "serverInfo": {"name": "mcp-tool-server", "version": "0.1.0"}}}
{"jsonrpc": "2.0", "id": 2, "result": {"content": [{"type": "text", "text": "84"}], "isError": false}}
```
To use it from a real client, point the client's MCP config at
`python /path/to/main.py` — the tools appear in the client like any other
MCP server.
## Architecture
```mermaid
flowchart LR
A[MCP client<br/>IDE / agent / desktop app] -->|JSON-RPC on stdio| B[protocol.py<br/>handshake + routing]
B --> C[tools.py<br/>calculator · word_count<br/>note_add · note_search]
C --> D[(notes.json)]
```
## Quick start
```bash
git clone https://github.com/Butterfly-ai-studios/mcp-tool-server.git
cd mcp-tool-server
python -m unittest discover -s tests # protocol + tools, 8 tests
python main.py # serve on stdio (waits for a client)
```
## Configuration
| Variable | Default | Purpose |
|---|---|---|
| `NOTES_FILE` | `notes.json` | Where the notes tools persist data |
## How this maps to a production system
MCP is becoming the standard tool surface for agents, and the production
version of this server differs in transport and tools, not in shape:
- **Transports** — streamable HTTP with auth, instead of local stdio
- **Real tools** — database queries, SaaS APIs, internal services, each with its own permission scope
- **The same discipline** — schemas up front, argument validation, tool errors returned as results the model can react to
My `tool-calling-agent` project shows the other side of the wire: an agent
consuming tools with exactly this contract.
## Project structure
```
server/
protocol.py JSON-RPC routing: initialize, tools/list, tools/call, ping
tools.py the four tools + their schemas
main.py stdio entry point
tests/ handshake, tool-call, error and stream tests
```
## License
MIT — see [LICENSE](LICENSE).
---
Built by [Masud Hossen](https://github.com/Butterfly-ai-studios) — AI product builder (VOOQIO, Foundqio). Part of a 10-project AI engineering portfolio.
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues