MCP Demo

MCP Demo

A model-agnostic MCP server exposing example tools (add1, multiply2, greet) for learning purposes, working with any LLM through stdio transport.

Category
访问服务器

README

MCP Demo

A learning project for the Model Context Protocol (MCP). It has one MCP server that exposes a few example tools, and two clients that let a language model discover and call those tools — one driven by a local model via LM Studio (free, offline) and one driven by Claude (Anthropic API).

The point of the project is to show that the server is model-agnostic: the same server.py works with any model — only the client changes.

How it fits together

  You type a question
          │
          ▼
   client (drives the LLM)  ──►  LLM decides to call a tool
          │                              │
          │  spawns over stdio           │
          ▼                              ▼
      server.py  ◄───────────  client executes the tool call
   (add1, multiply2, greet)     and feeds the result back
  • server.py — the MCP server (built with FastMCP). Defines the tools.
  • client_lmstudio.py — chat loop that drives a local model (LM Studio).
  • client_claude.py — same idea, driven by Claude (needs an API key).

The client launches server.py as a subprocess and talks to it over stdio (standard in/out). It lists the server's tools, hands them to the model, and runs the loop: model asks to call a tool → client runs it against the server → result goes back to the model → repeat until the model gives a final answer.

Tools exposed by the server

Tool What it does
add1(a, b) Returns a + b + 1 (the extra +1 proves the tool actually ran)
multiply2(a, b) Returns a * b * 2
greet(name, excited=False) Returns a greeting; excited=True adds enthusiasm

There's also a config://version resource returning the app version.

These are placeholder tools. To make the project do something real, replace the bodies with actual logic (a DB query, an API call, a computation) — the wiring stays exactly the same.

Setup

Requires Python 3.10+.

python3 -m venv .venv
source .venv/bin/activate

pip install -r requirements.txt

That installs everything: mcp[cli] (the server + Inspector), openai (the LM Studio client), and anthropic[mcp] (the Claude client — only needed if you use that one).

Running with a local model (LM Studio) — free, no API key

This is the recommended way to try it — it runs entirely on your machine.

  1. Install and open LM Studio.

  2. Download a model that supports tool/function calling. This project uses qwen2.5-7b-instruct-1m, which handles tools well. Other good picks: Qwen2.5 Instruct, Llama 3.1/3.2 Instruct. Small or non-instruct models often call tools poorly.

  3. Start the local server: LM Studio → Developer / Local Server tab → load the model → Start Server. It listens on http://localhost:1234/v1 (an OpenAI-compatible endpoint).

  4. Check the model id shown in LM Studio matches the MODEL constant near the top of client_lmstudio.py; update it if you loaded a different model.

  5. Run the client:

    .venv/bin/python client_lmstudio.py
    
  6. Chat. Type a question at the You: prompt, e.g. "Greet Alice excitedly, then add 5 and 7." Type exit or quit to leave.

If the "5 + 7" answer comes back as 13 (not 12), that confirms the model really called your add1 tool instead of doing its own arithmetic.

Notes on local models: they follow instructions less reliably than cloud models — responses may be verbose or occasionally skip a tool. Verbosity is tuned via the system prompt at the top of the client's messages list. Swapping to a stronger model in LM Studio also helps.

Running with Claude (Anthropic API) — costs a small amount

  1. Get an API key at console.anthropic.com (Settings → API Keys).

  2. Export it:

    export ANTHROPIC_API_KEY=sk-ant-...
    
  3. Run:

    .venv/bin/python client_claude.py
    

Same behavior, but Claude follows instructions and calls tools more reliably. Costs are tiny (a fraction of a cent for this demo).

Inspecting the server directly (no LLM)

To poke the tools by hand in a browser UI:

.venv/bin/mcp dev server.py

This opens the MCP Inspector, where you can call each tool manually. (If it errors trying to spawn uv, either install uv, or in the Inspector set Command to your venv's python and Arguments to server.py.)

Things worth remembering

  • The client and server transport must match. These clients spawn the server and talk over stdio, so server.py must end with mcp.run(). If you switch it to mcp.run(transport="sse") (an HTTP server), the stdio client breaks — it'll try to parse the web-server's log lines as protocol messages.
  • In stdio mode, don't print() inside tools — stdout is the protocol channel. Use print(..., file=sys.stderr) for debugging.
  • The server never changes when you switch models. MCP standardizes the tool server; each model provider just needs its own client (or a framework that abstracts them).

Possible next steps

  • Replace the placeholder tools with real functionality.
  • Add a graphical UI (Streamlit / Gradio / Chainlit for a quick chat box, or FastAPI + a web frontend for a real webapp).
  • Serve the tools over HTTP (transport="streamable-http") so they can be reached remotely instead of only as a local subprocess.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选