mcp-server-example

mcp-server-example

Minimal template MCP server with OAuth login and example tools for building custom servers on the Connext platform.

Category
访问服务器

README

mcp-server-example

A minimal, well-commented MCP server that connects to the Connext platform.

It demonstrates everything you need to build your own MCP server that your users can connect to from Connext:

  • 🔐 Its own login & OAuth — the server is its own OAuth 2.1 provider, with a simple username/password login page. A Connext user clicks "Connect", signs in to your server, and Connext receives an access token on their behalf.
  • 🛠️ An example tool (roll_dice) — an ordinary tool that returns text.
  • 🎨 An example MCP App (greeting_card) — a tool that returns a small HTML UI which Connext renders inline in the chat.
  • 👤 Per-user identity — tools know which of your users is calling.

It is built on FastMCP, which handles the MCP protocol and the standard OAuth plumbing, so the only code you have to understand is the ~150 lines that are specific to your application: your users, your login page, and your tools.


How it works

When a user connects this server in Connext, this is the flow (all standard OAuth 2.1 — Connext drives it automatically):

  Connext platform                         This MCP server
  ────────────────                         ───────────────
  1. discover ─────────────────────────▶  GET /.well-known/oauth-protected-resource/mcp
                                           GET /.well-known/oauth-authorization-server
  2. register (RFC 7591) ──────────────▶  POST /register            ← no manual client setup
  3. send user to log in ──────────────▶  GET  /authorize
                                           └▶ redirects the user's browser to:
  4. user signs in ────────────────────▶  GET/POST /login           ← YOUR login page
  5. get a token ──────────────────────▶  POST /token
  6. call tools (Authorization: Bearer)▶  POST /mcp                  ← your tools run

You only write step 4 (the login page) and step 6 (the tools). FastMCP gives you 1, 2, 3 and 5 for free.


Quick start

Requires Python 3.11+.

# 1. install
python -m venv .venv && source .venv/bin/activate
pip install -e .

# 2. run the server
python server.py
# -> serving on http://localhost:8000  (MCP endpoint: http://localhost:8000/mcp/)

# 3. in another terminal, connect to it like a real client would
python examples/connect_with_client.py
# -> opens your browser to the login page; sign in as  alice / password123

Demo users live in auth.py:

username password
alice password123
bob hunter2

Connecting it to Connext

  1. Expose the server on a public HTTPS URL. Connext must be able to reach your server's OAuth discovery endpoints, and for security it rejects private/loopback addresses for those endpoints. For a quick test, tunnel your local server:

    # example with cloudflared / ngrok — any tunnel works
    ngrok http 8000
    

    Then run the server with PUBLIC_URL set to the tunnel URL, because every OAuth endpoint it advertises is built from PUBLIC_URL:

    PUBLIC_URL=https://your-tunnel.example.com python server.py
    
  2. Register it in Connext (Admin → MCP Servers → Add):

    • URL: https://your-tunnel.example.com/mcp
    • Transport: HTTP
    • Auth: OAuth
    • Leave client id/secret blank — this server supports Dynamic Client Registration, so Connext registers itself automatically.
    • To let the greeting_card MCP App render, enable Allow UI on the server.
  3. Connect as a user. Each user clicks Connect, signs in on your login page, and Connext stores their token. Now the agent can call roll_dice and greeting_card as that user.


The files

File What it does
server.py Entry point. Reads config, builds the FastMCP server, wires in auth + tools, runs it.
auth.py The OAuth provider. Subclasses FastMCP's in-memory provider and adds one thing: a real login page (authorize()/login). Contains the demo user store.
tools.py The two example tools and the ui:// resource for the MCP App.
examples/connect_with_client.py A standalone client that runs the same OAuth flow Connext does — handy for testing.
.env.example Configuration (PUBLIC_URL, HOST, PORT).

What an MCP App is

An MCP App is just a tool whose result includes a ui:// resource carrying HTML. Connext renders that HTML in a sandboxed iframe inside the chat. The two pieces (see tools.py):

# 1. mark the tool as having a UI
@mcp.tool(meta={"ui": {"resourceUri": "ui://acme/greeting-card", ...}})
async def greeting_card(message: str) -> ToolResult:
    return ToolResult(
        content=[
            TextContent(text="..."),                         # what the model reads
            EmbeddedResource(resource=TextResourceContents(  # what the user sees
                uri="ui://acme/greeting-card",
                mimeType="text/html;profile=mcp-app",
                text="<!doctype html>...")),
        ],
        structured_content={"username": ..., "message": ...},
    )

# 2. also serve the template via resources/read
@mcp.resource("ui://acme/greeting-card", mime_type="text/html;profile=mcp-app", ...)
async def greeting_card_template() -> str:
    return "<!doctype html>..."

Keep the HTML self-contained (inline CSS, no external scripts) so it works under the iframe's strict Content-Security-Policy. Use the var(--mcp-color-*) CSS variables to match the host's light/dark theme.


Taking it to production

This example keeps everything in memory so it's easy to read. For a real server:

  • Users: replace the DEMO_USERS dict in auth.py with your real user database, and store hashed passwords (bcrypt/argon2) — or delegate to your existing SSO/identity provider.
  • Tokens: InMemoryOAuthProvider keeps tokens in memory, so they are lost on restart (users would re-connect). For production, persist them or issue signed JWTs (fastmcp.server.auth.providers.jwt).
  • HTTPS: terminate TLS in front of the server and set PUBLIC_URL to the https:// URL.
  • Scopes: this example uses a single read scope. Add scopes and enforce them per-tool via FastMCP's required_scopes / auth options.

Verified flow

This example was tested end-to-end: dynamic client registration → login (wrong password rejected, correct password accepted) → token exchange → authenticated tools/list and tools/call → token refresh → rejection of unauthenticated calls. Tools correctly see the signed-in user via get_access_token().subject.

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选