Skip to main content
Glama
Sdaas

mcp-tutorial

by Sdaas
README.md

# One time setup

How we setup this repo

```bash
uv init mcp-tutorial
cd mcp-tutorial
uv add "mcp[cli]<2.0"
```
# The MCP Server

`server.py` contains the MCP Server. It is a one line changes to switch it from `stdio` transport
to `streamable http` transport

```
mcp.run()  # stdio transport (subprocess)
-OR-
mcp.run(transport="streamable-http")  # now an HTTP service on :8000
```

# The MCP Client

The MCP client `host_with_loop.py` can work with both `stdio` and `streamable-http` transport

```
# If the MCP Server were using stdio transport, then we would use stdio_client(server_params) to talk to it.
async with stdio_client(server_params) as (read, write):

# If it were using HTTP transport, we would use streamablehttp_client("http://localhost:8000/mcp")
async with streamablehttp_client("http://localhost:8000/mcp") as (read, write, _):
```

# Testing with STDIO
- Make sure that `server.py` uses `mcp.run()  # stdio transport (subprocess)`
- Make sure that `host_with_loop` uses the stdio transport

## Testing the MCP Server 
Launch the inspector UI ( in browser )
```bash
uv run mcp dev server.py
```

## Testing the stand-alone MCP Client

This client runs the MCP Server as a subprocesses and then calls the tool. The 
key line is 
```
# Tell the client HOW to launch the server (as a subprocess over stdio)
server_params = StdioServerParameters(command="python", args=["server.py"])
```

Run it as 
```bash
uv run client_only.py
```

## Testing with Ollama
Check that Ollama is running 

```bash
$ ollama ps
NAME    ID    SIZE    PROCESSOR    CONTEXT    UNTIL

$ brew services list
Name          Status  User  File
ollama        started sdaas ~/Library/LaunchAgents/homebrew.mxcl.ollama.plist
...

$ curl http://localhost:11434/api/tags | jq
.... API response ....
```
### Simple host

Run the host
```bash
uv run host.py
```

### Host with Loop
```bash
uv run host_with_loop.py
```

# Testing with STDIO
- Make sure that `server.py` uses http transport
- Make sure that `host_with_loop` uses the http transport

Start the MCP server
```
$ uv run server.py
INFO:     Started server process [75425]
INFO:     Waiting for application startup.
[08/24/26 17:09:14] INFO     StreamableHTTP session manager started        streamable_http_manager.py:143
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
```

Run the application

```
uv run host_with_loop.py
USER: What's the temperature in Tokyo?
MODEL: ...
MODEL: ...
ANSWER: The current temperature in Tokyo is 18°C.
```