Skip to main content
Glama
vicboma1

claude-ia-mcp-tools-auth

by vicboma1

使用 OAuth 认证的 MCP 工具

一个 Python 示例,演示如何构建带有 OAuth 认证的 MCP(Model Context Protocol)服务器,将 API 客户端、业务逻辑层和受保护的 MCP 工具结合在一起。

功能特点

  • OAuth 认证流程:在浏览器中点击即可完成认证,获取会话令牌

  • 分层架构:API 客户端 → 业务逻辑 → MCP 工具

  • 安全工具访问:调用受保护的工具需要有效的认证令牌

  • 简单 HTTP 服务器:基于 Flask 的认证服务器,运行在 localhost:5000 上

  • 令牌管理:24 小时会话令牌,支持持久化

Related MCP server: OAuth MCP Server

架构

src/example/
├── api/
│   ├── api_client.py      # HTTP API client (JSONPlaceholder)
│   └── http_server.py     # Local HTTP server
├── auth/
│   └── manager.py         # OAuth token & state management
├── business/
│   └── service.py         # Business logic layer
├── http/
│   └── auth_server.py     # Flask OAuth auth server
├── mcp/
│   └── server.py          # MCP server with auth
└── main.py

安装

python -m venv .venv

# Windows:
.venv\Scripts\activate

# Linux/macOS:
source .venv/bin/activate

pip install -r requirements.txt

快速开始

1. 启动认证服务器

python -m src.example.http.auth_server

这会在 http://localhost:5000 上启动一个带 OAuth 流程的 Flask 服务器:

  • 访问首页

  • 点击“Click to Authenticate”按钮

  • 在回调页面上获取你的会话令牌

  • 复制并保存令牌

2. 启动 MCP 服务器

在另一个终端中:

python -m src.example.mcp.server

3. 使用 MCP 工具

MCP 服务器现在要求认证。首先,获取认证 URL:

echo '{"jsonrpc":"2.0","method":"tools/list","id":1}' | python -m src.example.mcp.server

然后使用你的令牌进行认证并调用工具:

echo '{"jsonrpc":"2.0","method":"tools/call","params":{"name":"get_user","arguments":{"user_id":1},"auth_token":"YOUR_SESSION_TOKEN"},"id":1}' | python -m src.example.mcp.server

认证流程

  1. 获取认证 URL:调用 get_auth_url 工具(无需认证)

    {"jsonrpc":"2.0","method":"tools/call","params":{"name":"get_auth_url","arguments":{}},"id":1}
  2. 在浏览器中点击:用户点击返回的认证 URL

    • 打开 http://localhost:5000/auth/callback?state=...

    • 浏览器显示成功页面并带会话令牌

    • 令牌有效期为 24 小时

  3. 使用令牌:在所有工具调用中包含 auth_token

{"params":{"name":"get_user","arguments":{"user_id":1},"auth_token":"YOUR_TOKEN"}}

可用工具

公开工具(无需认证)

  • get_auth_url - 获取 OAuth 认证 URL

受保护工具(需要认证)

  • get_user - 按 ID 获取单个用户

  • list_users - 列出所有用户

  • create_user - 创建新用户

  • update_user - 更新用户的姓名/email

  • delete_user - 删除用户

配置

设置环境变量:

export PORT=5000                           # Auth server port
export FLASK_SECRET_KEY=your-secret-key    # Flask secret (change in production!)

测试

使用 pytest 运行测试:

pytest -v
pytest --cov=src           # With coverage
pytest tests/test_auth.py  # Auth tests only

运行 shell 脚本

sh test-auth-flow.sh
========================================
  MCP Auth Server - Complete Flow Test
========================================
Base URL: https://claude-ia-mcp-tools-auth-staging.up.railway.app

Step 1: Start Auth Flow
GET /auth/start
Status: 401
Auth URL: https://claude-ia-mcp-tools-auth-staging.up.railway.app/auth/callback?state=Xukdt6MwHba0n0UfkOX3lAAanm7MJhSyzomyCJCxj1M

State Token: Xukdt6MwHba0n0UfkOX3lAAanm7MJhSyzomyCJCx...

Step 2: Complete Auth Callback
GET /auth/callback?state=Xukdt6MwHba0n0UfkOX3lAAanm7MJhSyzomyCJCxj1M
Status: 200
Session Token: 7Y6SaanfrLmiOXoE2kUvTbdEfawIMSJyGDaNFPf1...

Step 3: Verify Token with Auth Status
GET /auth/status -H 'Authorization: Bearer 7Y6SaanfrLmiOXoE2kUvTbdEfawIMSJyGDaNFPf1-Bg'
Response:
{"authenticated":true,"user_id":"user_1b25e4982c9904b8"}

========================================
         TEST RESULTS
========================================
State Token:     Xukdt6MwHba0n0UfkOX3lAAanm7MJhSyzomyCJCxj1M
Session Token:   7Y6SaanfrLmiOXoE2kUvTbdEfawIMSJyGDaNFPf1-Bg
Authenticated:   true
User ID:         user_1b25e4982c9904b8
========================================

Step 4: Test Invalid Token
GET /auth/status -H 'Authorization: Bearer invalid_token_123'
Response: {"authenticated":false,"user_id":null}

SUCCESS: Complete auth flow working correctly!

You can now use this token for MCP:
Authorization: Bearer 7Y6SaanfrLmiOXoE2kUvTbdEfawIMSJyGDaNFPf1-Bg

部署

部署到生产环境时,请更新:

  1. FLASK_SECRET_KEY - 使用强随机密钥

  2. OAuth 提供方 - 替换为真实的 OAuth(Google、GitHub 等)

  3. 令牌存储 - 使用数据库替代 .auth_tokens.json

  4. HTTPS - 为认证端点启用 SSL/Tls

架构说明

本示例展示了:

  • 关注点分离:API 客户端、业务逻辑层和 MCP 层相互独立

  • 分层设计:组件易于测试和替换

  • 认证集成:认证令牌通过参数传递,而非请求头部

  • 异常处理:对认证失败返回适当错误响应

API 客户端使用 https://jsonplaceholder.typicode.com 作为演示 API。 无需修改 MCP 与业务接口,即可替换为你自己的 API 实现。

F
license - not found
Not graded
quality - not tested
B
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • F
    license
    Not graded
    quality
    D
    maintenance
    A complete OAuth 2.1 server implementation for FastMCP with PKCE support, enabling secure authentication and authorization flows. Provides authorization code exchange, token management, and refresh capabilities for building authenticated MCP applications.
  • F
    license
    Not graded
    quality
    D
    maintenance
    An MCP server for OAuth 2.0 authentication supporting Device Code and Client Credentials flows, enabling secure token management for MCP applications.

View all related MCP servers

Related MCP Connectors

  • Self-hosted federated MCP gateway: one OAuth 2.1 MCP server in front of N apps, user-level scopes.

  • MCP server for Argo RPG Platform — connects AI assistants to campaign data via OAuth2

  • Streamable HTTP MCP server for Google Calendar and Sheets with OAuth login.

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/vicboma1/claude-ia-mcp-tools-auth'

If you have feedback or need assistance with the MCP directory API, please join our Discord server