MCP - Getting Started

This is a MCP Getting Started, to set up the simplest mcp server and go through the simple scenario.

1mkdir my-mcp-server
2cd my-mcp-server
3
4python -m venv venv
5source venv/bin/activate
6
7pip install fastmcp
8pip install pytz

Create server.py

 1from fastmcp import FastMCP
 2
 3mcp = FastMCP("我的第一个MCP服务器")
 4
 5@mcp.tool
 6def greet(name: str) -> str:
 7    return f"你好,{name}!欢迎使用MCP服务器。"
 8
 9@mcp.tool
10def add_numbers(a: int, b: int) -> int:
11    return a + b
12
13@mcp.tool
14def get_current_time(timezone: str = "Asia/Shanghai") -> str:
15    from datetime import datetime
16    import pytz
17    tz = pytz.timezone(timezone)
18    return datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
19
20if __name__ == "__main__":
21    mcp.run(transport="stdio")

Create test_client.py

 1# test_client.py
 2import asyncio
 3from fastmcp import Client
 4
 5async def main():
 6    async with Client("server.py") as client:
 7        tools = await client.list_tools()
 8        print("可用工具:", [t.name for t in tools])
 9
10        result = await client.call_tool("greet", {"name": "小明"})
11        print(result.content[0].text)
12
13asyncio.run(main())

There is no need to launch the server, which is handy for debugging purpose. You can also config in the Claude Code to connect the mcp through python + file name. This way is not suitable in prod environment since the file cannot serve multiple clients, mainly we need to lauch the mcp server.

Update server.py v2

1...
2if __name__ == "__main__":
3    mcp.run(
4        transport="streamable-http",
5        host="127.0.0.1",
6        port=8000,
7        path="/my_test_mcp",
8    )

Update test_client.py v2

1...
2async with Client("http://127.0.0.1:8000/my_test_mcp") as client:
3...

Optional: can also install MCP Inspector for debugging

1npx -y @modelcontextprotocol/inspector

Connect Claude Code to MCP Open ~.claude.json Insert content

1      "mcpServers": {
2        "mymcphttpserver": {
3          "type": "http",
4          "url": "http://127.0.0.1:8000/my_test_mcp"
5        }
6      },

Then open claude code, type /mcp you can see the mcp and its tools. Try to input "greet miao", the relevant greet tool from the mcp server will get triggered and return the result.

Written by Binwei@Suzhou