File size: 1,968 Bytes
84a13ac
 
 
 
 
 
 
 
 
 
 
 
ada812e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
---
title: MCP Server Demo
emoji: 🛰️
colorFrom: indigo
colorTo: blue
sdk: docker
app_file: Dockerfile
pinned: false
---


# Simple MCP Server and Client

There are two ways to implement

1: The easy way with FastMCP - Recommended

All the samples in the [MCP Python SDK](https://github.com/modelcontextprotocol/python-sdk) are based on FastMCP.


2: Directly with the SDK and an ASGI provider like [Starlette](https://www.starlette.io/) and then with uvicron

Needless to say the former is easy, the latter is difficult has more boilerplate code and very verbose.

## Package Manager used

Note 'uv' is the package manager used
Note `uv sync` and `source .venv/bin/activate` to run in the virtual environment with all dependecies

or run with `uv run`

## MCP Server and Client via  FastMCP


### Running the Server
```
 python mcp/fastmcp_server.py 
```

### Running the  Client 

```
python mcp/fastmcp_client.py 
```

**Output** - which is the real meat; this can be fed to the LLM and LLM will generate python code that be parsed by client and invoked as a tool call

```
Available tools: [Tool(name='add', description='Add two numbers', inputSchema={'properties': {'a': {'title': 'A', 'type': 'integer'}, 'b': {'title': 'B', 'type': 'integer'}}, 'required': ['a', 'b'], 'type': 'object'}, annotations=None)]
```
And for the call, the tool result  output

```
Tool result: [TextContent(type='text', text='3', annotations=None)]
```

## To Run in Docker

1. Build the Docker container

```
docker build -t fastmcp-server .
````

2. Run the Docker container

```
docker run -it --rm -p 7860:7860 fastmcp-server
```

3. Run the client

```
uv run fastmcp_client.py 
```





### MCP Server and Client directly (via ASGI container -Starlette) - Not Recommended

Running the Server

```
python mcp/mcp_server_starlette.py
```

Running the client Client

```
 python mcp/mcp_client.py 
```

The FastMCP client can also be used here. The output is the same as above