Token导航 LogoToken导航

MCP Server · 安全风控

MCP Sso Tutorial

基于FastAPI的MCP服务器,提供JWT认证和OAuth2动态客户端注册功能,支持Google OAuth集成。

stdio官方级别未说明来源级核验工具 6
FastAPIPython安全

快速开始

作者 / 组织
Othunderlight
提供方
Othunderlight

工具数量

6

来源目录标注

资源数量

0

来源目录标注

提示词数量

0

来源目录标注

GitHub Star

0

仓库热度

收录来源

1 个

MCP World

选型结论

MCP Sso Tutorial 属于安全风控类 MCP Server,公开资料标注 6 个工具,通过stdio 本地进程接入,客户端适配情况以官方文档为准,有可以直接复制的安装命令;接入前要先准备 OAuth 授权。

分类

安全风控

传输方式

stdio

认证方式

OAuth 授权

部署方式

公开资料未说明

安装与配置

安装命令

pip install -e .

公开命令不是通用的启动方式,拼不出可靠配置示例;请在客户端 MCP 配置里按官方文档填写 command 和 args。

上手步骤

  1. 1

    准备运行环境

    stdio 服务在本地起进程,需要先装好 对应的运行时,再按安装命令拉取依赖。

  2. 2

    安装服务

    复制下面的安装命令执行;命令里带占位符的位置换成自己的密钥或路径。

  3. 3

    写入客户端配置

    在客户端的 MCP 配置里填启动命令和参数,保存后完全退出客户端再打开,让新配置生效。

  4. 4

    验证是否可用

    连接成功后客户端会列出 6 个工具,随便调一个只读工具确认能返回结果。

接入前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

详细介绍

具有Django JWT身份验证和OAuth2的MCP服务器

这是一个基于FastAPI的MCP(模型上下文协议)服务器,它使用Django后端的JWT令牌以及Allauth和SimpleJWT提供安全身份验证。它支持 OAuth2动态客户端注册 使用自动Google OAuth流。

建筑

┌───────────────┐    OAuth2    ┌───────────────┐   Google    ┌───────────────┐           ┌───────────────┐
│ MCP Client    │────────────▶│ MCP Server    │───────────▶│ Django Backend│ ────────▶│ Google OAuth  │
│ (e.g. GPT)    │ Register+    │ (FastAPI)     │ Redirect    │ (Allauth+DRF) │ OAuth     │ Provider      │
│               │ Authorize    │               │             │               │           │               │
└───────────────┘              └───────────────┘             └───────────────┘           └───────────────┘
        │                             │                             │                         │
        │         JWT Access Token    │      JWT Token Exchange     │    User Info + Session  │
        └─────────────────────────────┼─────────────────────────────┼─────────────────────────┘
                                      │                             │
                                      └─────────────────────────────┘
                                             API Calls with JWT

特性

  • OAuth2动态客户端注册:MCP客户端的自动客户端注册
  • Google OAuth集成:通过Django的Google OAuth流进行无缝身份验证
  • JWT身份验证:使用RS256验证Django后端发出的令牌
  • JWKS支持:自动从Django的JWKS端点获取和缓存公钥
  • 用户上下文:将经过身份验证的用户信息附加到请求中
  • MCP工具:提供对Django后端API的身份验证访问
  • 缓存:具有可配置TTL的高效JWKS密钥缓存
  • 错误处理:正确的OAuth2错误响应
  • 遵从标准:支持PKCE的完整OAuth2授权代码流

设置

1.安装依赖项

cd mcp-auth-test
pip install -e .
# or using uv
uv pip install -e .

2.配置环境

复制示例环境文件并对其进行配置:

cp .env.example .env

编辑 .env 使用Django后端配置:

# Django Backend Configuration
DJANGO_BASE_URL=http://localhost:8000

# JWT Configuration (these should match your Django settings)
JWT_AUDIENCE=cccrm-api
JWT_ISSUER=cccrm-backend

# Optional: Provide static public key (otherwise JWKS will be used)
# JWT_PUBLIC_KEY=-----BEGIN PUBLIC KEY-----\n...\n-----END PUBLIC KEY-----

# Server Configuration
PORT=10000

# Optional: Tavily API for web search tool
TAVILY_API_KEY=your-tavily-api-key

3.Django后端设置

确保你的Django后端在 settings.py:

# JWT Configuration
SIMPLE_JWT = {
    'ALGORITHM': 'RS256',
    'SIGNING_KEY': env('JWT_PRIVATE_KEY'),
    'VERIFYING_KEY': env('JWT_PUBLIC_KEY'),
    'AUDIENCE': env('JWT_AUDIENCE', default='cccrm-api'),
    'ISSUER': env('JWT_ISSUER', default='cccrm-backend'),
    # ... other settings
}

# Expose JWKS endpoint
# Add this URL pattern to expose /.well-known/jwks.json

4.生成JWT密钥(如果需要)

如果你需要为Django后端生成RS256密钥对:

# generate_jwt_keys.py
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization

# Generate private key
private_key = rsa.generate_private_key(
    public_exponent=65537,
    key_size=2048
)

# Get public key
public_key = private_key.public_key()

## OAuth2 Dynamic Client Registration

The MCP server supports OAuth2 Dynamic Client Registration, allowing clients to automatically register and authenticate users via Google OAuth through Django.

### OAuth2 Flow
  1. 客户注册

MCP客户端──────────POST/oauth2/注册──────────▶ MCP服务器 │ ▼ 生成client_id/secret │ ▼ MCP客户端◀──────────client_id+客户端密码───────┘

  1. 授权请求\

MCP客户端──────────GET/oauth2/授权─────────▶ MCP服务器 │ ▼ 重定向到Django │ ▼ 用户浏览器────────/oauth/谷歌/?next=回调──▶ Django后端 │ ▼ 谷歌OAuth │ ▼ 用户浏览器◀谷歌 │ ▼ 用户身份验证 │ ▼ 用户浏览器─────────使用身份验证码进行回调──────▶ Django后端 │ ▼ Django回调 │ ▼ 用户浏览器─────────重定向到MCP回调─────▶ MCP服务器 │ ▼ 生成身份验证码 │ ▼ MCP客户端◀─────────授权代码─────────────── MCP服务器

  1. 代币交换

MCP客户端──────────POST/oauth2/令牌────────────▶ MCP服务器 (身份验证码+客户端凭据)│ ▼ 为JWT调用Django │ ▼ MCP客户端◀─────────JWT访问令牌──────────────── MCP服务器


### OAuth2 Endpoints

#### 1. Dynamic Client Registration

POST /oauth2/register Content-Type: application/json

{ "client_name": "My MCP Client", "client_uri": "https://example.com", "redirect_uris": ["https://example.com/callback"], "grant_types": ["authorization_code"], "response_types": ["code"], "scope": "openid profile email" }


**答复:**

{ "client_id": "mcp_client_abc123...", "client_secret": "secret_xyz789...", "client_name": "My MCP Client", "redirect_uris": ["https://example.com/callback"], "grant_types": ["authorization_code"], "response_types": ["code"], "scope": "openid profile email", "client_id_issued_at": 1699123456, "client_secret_expires_at": 0 }


#### 2.授权请求

GET /oauth2/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code&scope=openid%20profile%20email&state={state}&code_challenge={challenge}&code_challenge_method=S256


这将把用户重定向到:

Django: /oauth/google/?next=/mcp/oauth2/callback/?mcp_callback_params=...


#### 3.代币兑换

POST /oauth2/token Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code& client_id={client_id}& client_secret={client_secret}& code={authorization_code}& redirect_uri={redirect_uri}& code_verifier={verifier}


**答复:**

{ "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...", "token_type": "Bearer", "expires_in": 3600, "scope": "openid profile email", "refresh_token": "refresh_token_here..." }


### OAuth2发现

#### 授权服务器元数据

GET /.well-known/oauth-authorization-server


**答复:**

{ "issuer": "http://localhost:10000", "authorization_endpoint": "http://localhost:10000/oauth2/authorize", "token_endpoint": "http://localhost:10000/oauth2/token", "registration_endpoint": "http://localhost:10000/oauth2/register", "jwks_uri": "http://localhost:8000/auth/.well-known/jwks.json", "response_types_supported": ["code"], "grant_types_supported": ["authorization_code"], "token_endpoint_auth_methods_supported": ["client_secret_post"], "scopes_supported": ["openid", "profile", "email"], "code_challenge_methods_supported": ["S256", "plain"], "subject_types_supported": ["public"] }


#### 资源服务器元数据

GET /.well-known/oauth-protected-resource/mcp


**答复:**

{ "resource_server_metadata": { "resource_identifier": "cccrm-api", "authorization_servers": ["http://localhost:10000"], "authorization_server_metadata_url": "http://localhost:10000/.well-known/oauth-authorization-server", "jwks_uri": "http://localhost:8000/auth/.well-known/jwks.json", "audience": "cccrm-api", "issuer": "cccrm-backend", "oauth2_dynamic_registration": true, "registration_endpoint": "http://localhost:10000/oauth2/register" } }


# 序列化私钥

private_pem=private_key.private_bytes(
编码=序列化。编码。PEM,
格式=序列化。私人格式。PKCS8,
加密算法=序列化。无加密()
).decode()

# 序列化公钥

public_pem=public_key.public_bytes(
编码=序列化。编码。PEM,
格式=序列化。公共格式。主题公钥信息
).decode()

print(“私钥:”)
打印(private_pem)
print(“\\n公钥:”)
打印(public_pem)

Running the Server

# Using the entry point
mcp-server

# Or directly
python -m src.server

# Or with uvicorn
uvicorn src.server:app --host localhost --port 10000 --reload

认证流程

1.用户身份验证

用户首先通过Django后端进行身份验证:

# Login to get JWT token
curl -X POST http://localhost:8000/auth/login/ \
  -H "Content-Type: application/json" \
  -d '{"email": "user@example.com", "password": "password"}'

答复:

{
  "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...",
  "refresh_token": "...",
  "user": {
    "id": 1,
    "email": "user@example.com"
  }
}

2.MCP客户端连接

MCP客户端使用JWT令牌连接到服务器:

# Example MCP request
curl -X POST http://localhost:10000/ \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/call",
    "params": {
      "name": "get_user_profile",
      "arguments": {}
    }
  }'

可用工具

1. get_user_profile

获取当前已验证用户的个人资料信息。

{
  "method": "tools/call",
  "params": {
    "name": "get_user_profile",
    "arguments": {}
  }
}

2. list_campaigns

列出已验证用户的所有活动。

{
  "method": "tools/call",
  "params": {
    "name": "list_campaigns",
    "arguments": {}
  }
}

3. create_campaign

为经过身份验证的用户创建新活动。

{
  "method": "tools/call",
  "params": {
    "name": "create_campaign",
    "arguments": {
      "name": "My New Campaign",
      "description": "Campaign description"
    }
  }
}

4. get_leads_count

获取已验证用户的潜在客户总数。

{
  "method": "tools/call",
  "params": {
    "name": "get_leads_count",
    "arguments": {}
  }
}

5. search_web (可选)

使用Tavilly API搜索网页(需要Tavily_API_KEY)。

{
  "method": "tools/call",
  "params": {
    "name": "search_web",
    "arguments": {
      "query": "latest news about AI"
    }
  }
}

6. add_numbers

简单的演示工具,用于添加两个数字。

{
  "method": "tools/call",
  "params": {
    "name": "add_numbers",
    "arguments": {
      "a": 5,
      "b": 3
    }
  }
}

安全特性

JWT验证

  • RS256算法:使用非对称加密技术进行安全令牌验证
  • JWKS集成:自动从Django的JWKS端点获取公钥
  • 索赔验证:验证发行人、受众和到期声明
  • 缓存:缓存具有可配置TTL的JWKS密钥以提高性能

请求上下文

中间件将用户信息附加到每个请求中:

request.state.user = {
    "id": payload.get("user_id"),
    "email": payload.get("email"),
    "username": payload.get("username"),
    "token_type": payload.get("token_type"),
    "exp": payload.get("exp"),
    "iat": payload.get("iat"),
    "jti": payload.get("jti"),
}

错误处理

服务器返回正确的OAuth2错误响应:

{
  "error": "invalid_token",
  "error_description": "Token has expired"
}

常见错误类型:

  • invalid_token:令牌格式错误、已过期或无效
  • insufficient_scope:令牌没有所需的权限

发展

添加新工具

  1. 将您的工具功能添加到 src/tools.py:
@mcp.tool
async def my_new_tool(request: Request, param1: str, param2: int = 0) -> Dict:
    """Description of what this tool does"""
    if not hasattr(request.state, 'user') or not request.state.user:
        raise Exception("User not authenticated")
    
    # Your tool logic here
    return {"result": "success"}
  1. 工具可以自动访问:

- request.state.user:经过身份验证的用户信息 - request.headers.get("Authorization"):后端请求的原始JWT令牌

测试

# Install test dependencies
pip install pytest httpx pytest-asyncio

# Run tests (when available)
pytest tests/

配置参考

环境变量描述默认值
DJANGO_BASE_URLDjango后端的基本URLhttp://localhost:8000
JWT_AUDIENCE预计JWT观众人数cccrm-api
JWT_ISSUER预期JWT发行人索赔cccrm-backend
JWT_PUBLIC_KEY静态RSA公钥(可选)-
JWKS_CACHE_TTLJWKS缓存TTL(秒)3600
PORT服务器端口10000
TAVILY_API_KEYTavilly API网络搜索密钥-

故障排除

常见问题

  1. “无法获取JWKS密钥”

- 检查一下 DJANGO_BASE_URL 是正确的 - 确保Django后端正在运行且可访问 - 验证JWKS端点是否可用 /auth/.well-known/jwks.json

  1. “令牌颁发者/受众无效”

- 确保 JWT_ISSUER 和 JWT_AUDIENCE 匹配Django设置 - 检查令牌是否以正确的声明发放

  1. “令牌验证失败”

- 验证令牌未过期 - 检查Django和MCP服务器之间的JWT密钥是否匹配 - 确保使用RS256算法

调试

启用调试日志记录:

import logging
logging.basicConfig(level=logging.DEBUG)

许可证

\[您的许可证在这里\]

工具与能力

工具数量

6

资源数量

0

提示词数量

0

能力标签

JWT认证Python本地部署OAuth2动态客户端注册GoogleOAuthFastAPI

数量来自来源目录的标注,具体工具清单以官方文档为准。

典型使用场景

资产与漏洞排查

批量查询资产和漏洞信息,输出待修清单。

日志审计

从日志里筛出异常访问和高风险操作。

合规检查

按规则核对配置项,记录不符合项。

选择建议

值得选的地方

  • 只提供 6 个工具,职责集中,接入后不容易误操作。
  • 有现成的安装命令,不用自己找入口,照着跑就能起服务。
  • 公开说明写得比较完整,接入前能看清能力边界。

先想清楚的地方

  • stdio 方式要求本地装好 运行环境,换机器要重新装一遍。
  • 需要自备OAuth 授权,没有凭据没法直接试。
  • 公开摘要偏短,具体能力和限制要回到仓库确认。

更适合

  • 需要在安全风控场景里让模型直接调用外部能力的人。
  • 想先快速试一下、能接受在本地跑服务的人。

不太适合

  • 完全不想在本地装环境的人。
  • 暂时拿不到OAuth 授权的人。
  • 需要厂商级 SLA 和技术支持的生产环境。

同类对比

5 个同类
MCP Server工具数传输方式安装命令收录来源最后核验
MCP Sso Tutorial

MCP World · 本页

6stdio有1 个2026-05-17
Modelcontextprotocol C22988b0

MCP World

0stdio有1 个2026-05-17
Hexstrike AI

MCP World

0stdio有1 个2026-05-17
Cyber Strike AI

MCP World

75未说明未标注1 个2026-05-17
MCP Scan

MCP World

0stdio有1 个2026-05-17

按同分类和工具数量挑出最接近的几条,字段以各自详情页为准。

常见问题

5 条

MCP Sso Tutorial 怎么安装?

公开资料给出的安装命令是 pip install -e .,运行环境为 未标注。装完后按客户端要求写进 MCP 配置,重启客户端生效。

MCP Sso Tutorial 怎么在 Claude Desktop、Cursor 里配置?

它是stdio 本地进程,客户端配置里要写启动命令和参数,运行环境是 未标注;保存后完全退出客户端再打开,配置才会生效。

MCP Sso Tutorial 收费吗?

本站只整理公开资料,不代收费用,也没有标注具体价格。是否收费取决于服务本身和背后的接口额度,正式使用前按官方说明确认。

MCP Sso Tutorial 需要 API Key 吗?

公开资料标注的认证方式是OAuth 授权,也就是要先拿到凭据才能连上;凭据只填在自己本地的配置里,不要写进公开仓库。

MCP Sso Tutorial 支持哪些客户端?

公开资料没有标注具体客户端;能配置本地 MCP 命令的客户端都能接,配置方式参照官方文档。

来源信息

继续浏览同类 MCP