Skip to main content
Glama
vicboma1

claude-ia-mcp-tools-auth

by vicboma1

OAuth認証付きMCPツール

OAuth認証を備えたMCP(Model Context Protocol)サーバーの構築方法を示すPythonの例です。APIクライアント、ビジネスロジック層、保護されたMCPツールを組み合わせています。

特徴

  • OAuth認証フロー: ブラウザでクリックして認証し、セッショントークンを取得

  • レイヤードアーキテクチャ: APIクライアント → ビジネスロジック → MCPツール

  • 安全なツールアクセス: 保護されたツールを呼び出すには有効な認証トークンが必要

  • シンプルなHTTPサーバー: localhost:5000で動作するFlaskベースの認証サーバー

  • トークン管理: 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

これにより、OAuthフローを備えたFlaskサーバーがhttp://localhost:5000で起動します:

  • ホームページにアクセス

  • 「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でユーザーを1件取得

  • list_users - すべてのユーザーを一覧表示

  • create_user - 新しいユーザーを作成

  • update_user - ユーザーの名前/メールを更新

  • 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

シェルスクリプトを実行:

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クライアントはデモAPIとしてhttps://jsonplaceholder.typicode.comを使用しています。 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