mcpserver-demo / README.md
alexcpn's picture
added config for hf spaces
84a13ac
metadata
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 are based on FastMCP.

2: Directly with the SDK and an ASGI provider like Starlette 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 .
  1. Run the Docker container
docker run -it --rm -p 7860:7860 fastmcp-server
  1. 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