|
| 1 | +# Hello World MCP Example |
| 2 | + |
| 3 | +## Pre-requisites |
| 4 | + |
| 5 | +- [uv](https://github.com/astral-sh/uv) (Fast Python package installer and resolver) |
| 6 | + |
| 7 | +## Setup |
| 8 | + |
| 9 | +Install dependencies: |
| 10 | +- ruff install info at https://docs.astral.sh/ruff/installation/ |
| 11 | +```bash |
| 12 | +uv pip install -e ".[dev]" && uv sync |
| 13 | +``` |
| 14 | + |
| 15 | +## Running the Server |
| 16 | + |
| 17 | +Run the MCP server (configured to use SSE transport): |
| 18 | + |
| 19 | +```bash |
| 20 | +uv run src/main.py |
| 21 | +``` |
| 22 | + |
| 23 | +- The server will start on `http://127.0.0.1:8000` |
| 24 | + |
| 25 | +## Testing |
| 26 | + |
| 27 | +### In Browser: Validating the Server |
| 28 | + |
| 29 | +- [http://127.0.0.1:8000](http://127.0.0.1:8000) |
| 30 | + |
| 31 | + |
| 32 | +- [http://127.0.0.1:8000?name=krishna](http://127.0.0.1:8000?name=krishna) |
| 33 | + |
| 34 | + |
| 35 | +### Unit Testing |
| 36 | + |
| 37 | +```bash |
| 38 | +uv run pytest tests/maintests.py -v |
| 39 | +``` |
| 40 | + |
| 41 | + |
| 42 | +### Manual Testing via Curl (MCP Protocol) |
| 43 | + |
| 44 | +To test the MCP protocol fully via `curl`, you need to simulate an MCP client. This involves two steps: |
| 45 | + |
| 46 | +**1. Connect to SSE Stream** |
| 47 | + |
| 48 | +Open a terminal and listen to the event stream: |
| 49 | + |
| 50 | +```bash |
| 51 | +curl -N http://127.0.0.1:8000/sse |
| 52 | +``` |
| 53 | + |
| 54 | + |
| 55 | +*Keep this running.* You will see an `endpoint` event containing a URL (e.g., `/messages/?session_id=...`). Copy this URL. |
| 56 | + |
| 57 | +**2. Send JSON-RPC Requests** |
| 58 | + |
| 59 | +In a **new terminal**, use the URL you copied to send requests. |
| 60 | + |
| 61 | +**Initialize:** |
| 62 | + |
| 63 | +```bash |
| 64 | +curl -X POST "http://127.0.0.1:8000/messages/?session_id=123456" \ |
| 65 | + -H "Content-Type: application/json" \ |
| 66 | + -d '{ |
| 67 | + "jsonrpc": "2.0", |
| 68 | + "id": 1, |
| 69 | + "method": "initialize", |
| 70 | + "params": { |
| 71 | + "protocolVersion": "2024-11-05", |
| 72 | + "capabilities": {}, |
| 73 | + "clientInfo": { "name": "curl-client", "version": "1.0" } |
| 74 | + } |
| 75 | + }' |
| 76 | +``` |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +**List Tools:** |
| 83 | + |
| 84 | +```bash |
| 85 | +curl -X POST "http://127.0.0.1:8000/messages/?session_id=123456" \ |
| 86 | + -H "Content-Type: application/json" \ |
| 87 | + -d '{ |
| 88 | + "jsonrpc": "2.0", |
| 89 | + "id": 2, |
| 90 | + "method": "tools/list" |
| 91 | + }' |
| 92 | +``` |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +**Call Tool (index):** |
| 97 | + |
| 98 | +```bash |
| 99 | +curl -X POST "http://127.0.0.1:8000/messages/?session_id=123456" \ |
| 100 | + -H "Content-Type: application/json" \ |
| 101 | + -d '{ |
| 102 | + "jsonrpc": "2.0", |
| 103 | + "id": 3, |
| 104 | + "method": "tools/call", |
| 105 | + "params": { |
| 106 | + "name": "index", |
| 107 | + "arguments": {} |
| 108 | + } |
| 109 | + }' |
| 110 | +``` |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +**Call Tool (greeting):** |
| 116 | + |
| 117 | +```bash |
| 118 | +curl -X POST "http://127.0.0.1:8000/messages/?session_id=123456" \ |
| 119 | + -H "Content-Type: application/json" \ |
| 120 | + -d '{ |
| 121 | + "jsonrpc": "2.0", |
| 122 | + "id": 4, |
| 123 | + "method": "tools/call", |
| 124 | + "params": { |
| 125 | + "name": "greeting", |
| 126 | + "arguments": { "name": "Krishna" } |
| 127 | + } |
| 128 | + }' |
| 129 | +``` |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +### MCP Inspector |
| 135 | + |
| 136 | +The [MCP Inspector](https://github.com/modelcontextprotocol/inspector) is a developer tool for testing and debugging MCP servers. |
| 137 | + |
| 138 | +Run the inspector (this uses `npx` to run the inspector, which then runs your server in `stdio` mode): |
| 139 | + |
| 140 | +```bash |
| 141 | +npx @modelcontextprotocol/inspector uv run src/main.py --transport stdio |
| 142 | +``` |
0 commit comments