Skip to content

27 Add mcp server#28

Merged
zucky2021 merged 4 commits intomainfrom
feature/27-implement-sse
Dec 24, 2025
Merged

27 Add mcp server#28
zucky2021 merged 4 commits intomainfrom
feature/27-implement-sse

Conversation

@zucky2021
Copy link
Owner

@zucky2021 zucky2021 commented Dec 23, 2025

Summary by CodeRabbit

  • 新機能

    • サーバーセントイベント(SSE)によるリアルタイムストリーミングを追加。/api/sse/stream と /api/sse/countdown で定期配信とカウントダウン配信が可能になりました。
  • その他の変更

    • 開発設定の見直し:サーバーエントリ名と環境参照方法を更新し、プロジェクト指定を固定値に変更しました。
    • サンプル環境ファイルを更新(プロジェクトIDの扱いとアクセス令牌の記載)。
    • 依存関係の更新:主要ライブラリのバージョン上げとセキュリティ関連パッケージ追加。

✏️ Tip: You can customize this high-level summary in your review settings.

@zucky2021 zucky2021 linked an issue Dec 23, 2025 that may be closed by this pull request
@zucky2021 zucky2021 self-assigned this Dec 23, 2025
@coderabbitai
Copy link

coderabbitai bot commented Dec 23, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

レビュー: MCP サーバー設定名を apidog から API specification に変更し、サーバー引数を固定プロジェクト値に更新、環境設定を .env 参照に置換しました。バックエンドに新しい SSE ルーターを追加し、/api/sse に 2 つのストリーミングエンドポイントを公開。依存で langchain-core を更新し、marshmallowfilelock を追加しました。

Changes

コホート / ファイル(群) 変更サマリー
MCP サーバー設定
.cursor/mcp.json
サーバー識別名を apidogAPI specification に変更。引数リストの --project-id=${env:APIDOG_PROJECT_ID}--project=1134437 に置換。環境設定ブロックを env から envFile: ${workspaceFolder}/.env に切替。
環境サンプル
.env.example
APIDOG_PROJECT_ID=1058162 を削除し、APIDOG_ACCESS_TOKEN= を追加(空)。DB 系の行はそのまま。
バックエンド起動 / ルーター登録
backend/app/main.py
app.presentation.routers.sse をインポートし、app.include_router(sse.router, prefix="/api/sse", tags=["sse"]) で SSE ルーターを登録。
SSE 実装(新規)
backend/app/presentation/routers/sse.py
新規ファイル。/stream(5 件を1秒間隔で送信、最後に DONE)と /countdown/{count}(最大 60、カウントダウンを SSE イベントで送信)を実装。キャッシュ無効化・バッファリング制御ヘッダーを付与。非同期ジェネレータをエクスポート。
依存関係
backend/pyproject.toml
langchain-core>=1.0.7>=1.2.5 に更新。marshmallow>=3.26.2filelock>=3.20.1 を追加。

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Client as クライアント
  participant API as FastAPI (/api/sse)
  participant Gen as SSE ジェネレータ
  rect rgb(235,245,255)
    Note over Client,API: クライアントが SSE エンドポイントに接続
  end
  Client->>API: GET /api/sse/stream
  API->>Gen: 起動 event_generator()
  loop 5 回 (1秒間隔)
    Gen-->>API: "data: Message i..."
    API-->>Client: SSE フレーム (data)
  end
  Gen-->>API: "event: DONE"
  API-->>Client: SSE フレーム (DONE) と接続終了
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • 25 Implement mcp #26 — MCP サーバー設定追加とアプリ初期化での MCP マウントに関する変更と直接関連。

Poem

流れるイベント、刻む一秒、
/api/sse に小さな風が吹く🌬️
設定は .env に隠れて、
依存は新しく、波は静かに動く。

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Title check ⚠️ Warning プルリクエストのタイトル「27 Add mcp server」は、実際の変更内容(SSEルーターの追加とMCPサーバー設定の更新)と部分的に関連していますが、主要な変更点を正確に反映していません。 タイトルを「Add SSE router and update MCP server configuration」など、実際の主要な変更内容をより正確に反映した表現に変更することをお勧めします。
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ecf8b51 and 5127bfe.

⛔ Files ignored due to path filters (1)
  • backend/uv.lock is excluded by !**/*.lock
📒 Files selected for processing (1)
  • backend/pyproject.toml

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
backend/app/presentation/routers/sse.py (1)

57-74: ✅ カウントダウンエンドポイントの実装が適切です。

最大60秒の制限により長時間接続を防いでいる点が良いです。同様のヘッダー設定で一貫性も保たれています。

💡 オプション:クエリパラメータの検討

現在、countがパスパラメータとして定義されていますが、オプショナルな値として扱いたい場合はクエリパラメータの方が自然かもしれません:

@router.get("/countdown")
async def sse_countdown(count: int = 10) -> StreamingResponse:

ただし、現在の実装(パスパラメータ)でも機能的には問題ありません。

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e6c5347 and dbc30a1.

📒 Files selected for processing (4)
  • .cursor/mcp.json
  • .env.example
  • backend/app/main.py
  • backend/app/presentation/routers/sse.py
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-12-10T23:48:19.511Z
Learnt from: CR
Repo: zucky2021/ai-chatbot-api PR: 0
File: docs/design/directory-structure.md:0-0
Timestamp: 2025-12-10T23:48:19.511Z
Learning: Applies to docs/design/backend/app/presentation/routers/**/*.py : API routes should be organized in backend/app/presentation/routers/ with descriptive filenames indicating the endpoint domain (e.g., chat.py for chat endpoints, health.py for health check endpoints)

Applied to files:

  • backend/app/main.py
🔇 Additional comments (9)
.env.example (1)

2-2: ✅ 環境変数の更新が適切です。

APIDOG_PROJECT_IDからAPIDOG_ACCESS_TOKENへの変更は、.cursor/mcp.jsonの更新と一貫性があります。サンプルファイルとして空値のままで問題ありません。

.cursor/mcp.json (2)

18-18: ✅ サーバー名の変更が明確です。

"apidog"から"API specification"への変更により、MCPサーバーの目的がより明確になりました。


20-20: want: プロジェクトIDを環境変数で管理してください。

--project=1134437がハードコードされていますが、APIDOG_ACCESS_TOKENは環境変数を使用しています。プロジェクトIDも環境変数として管理することで、環境ごとに異なる設定に対応できるようになります。APIDOG_PROJECT_IDという環境変数を使用することをご検討ください。

Likely an incorrect or invalid review comment.

backend/app/main.py (2)

26-26: ✅ インポートが適切に追加されています。

SSEルーターのインポートが既存のパターンに従っており、アルファベット順に整理されています。


88-88: ✅ SSEルーターの登録が正しく実装されています。

ルーターの登録が既存のパターン(health、chat)と一貫性を保っており、適切なプレフィックス(/api/sse)とタグが設定されています。

Based on learnings、ルーターがbackend/app/presentation/routers/に配置されていることも確認できました。

backend/app/presentation/routers/sse.py (4)

1-12: ✅ モジュール構成が適切です。

必要なインポートのみが含まれており、ルーターの初期化も正しく行われています。モジュールドキュメントでApidogテスト用であることが明示されているのも良い点です。


15-26: ✅ SSEジェネレーターが正しく実装されています。

イベントジェネレーターが適切なSSEフォーマット(data: ...\n\n)を使用しており、1秒間隔で5つのメッセージを送信する実装は、テスト用途として適切です。[DONE]マーカーの使用も標準的な慣例に従っています。


29-43: ✅ SSEストリーミングエンドポイントが適切に設定されています。

StreamingResponseの設定が正しく、必要なヘッダー(Cache-ControlConnectionX-Accel-Buffering)がすべて含まれています。text/event-streamのメディアタイプも適切です。


46-54: ✅ カウントダウンジェネレーターの実装が正確です。

カスタムイベントタイプ(countdowncomplete)を使用したSSEフォーマットが正しく実装されており、クライアント側でのイベントフィルタリングが可能になっています。

.cursor/mcp.json Outdated
"env": {
"APIDOG_PROJECT_ID": "${env:APIDOG_PROJECT_ID}",
"APIDOG_ACCESS_TOKEN": "${env:APIDOG_ACCESS_TOKEN}"
"APIDOG_ACCESS_TOKEN": "${APIDOG_ACCESS_TOKEN}"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

# Search for the review guidelines document
find . -name "review.md" -path "*/development/coding-rules/*" 2>/dev/null

Repository: zucky2021/ai-chatbot-api

Length of output: 109


🌐 Web query:

MCP model context protocol environment variable syntax ${env:} vs ${}

💡 Result:

Short answer

  • Both ${env:VAR} and ${VAR} are used by MCP-related tooling to insert environment-variable values into MCP config fields; ${env:VAR} is the explicit form and ${VAR} is a shorter/shorthand form that some clients accept. Use ${env:VAR} when you want to be explicit and portable; check the MCP client’s docs because support and fallback semantics vary. [1][2][3]

Details and practical notes

  • Explicit form: ${env:VARIABLE} — clearly names the source as an environment variable; shown in several MCP/Claude-style config docs and examples. Use this for clarity and when a tool’s syntax list documents it. [3]
  • Shorthand: ${VARIABLE} or $VARIABLE — many MCP implementations accept the shorter form (often shown for uppercase env names) and support default-value syntax like ${VAR:-default}. Not every client supports both forms. [2][3]
  • Other interpolation syntaxes exist in MCP ecosystems (examples: ${file:...}, ${exec:...}, ${secret:...}) — these are different expansion mechanisms, not the same as env interpolation. [4][5]
  • Important: Some MCP clients/config formats do NOT perform any ${...} expansion at all (they require literal strings or rely on the process environment being set separately). Always consult the specific MCP client’s docs (or test). Example: a docs page that notes .mcp.json does not expand ${VAR}. [0][1]

Quick recommendations

  • Prefer ${env:NAME} for readability/portability.
  • If you rely on default-value plumbing (e.g., ${VAR:-fallback}) confirm the client supports bash-style defaults.
  • If unsure, test locally: run the MCP client with a config that references ${env:...} and verify the server receives the intended value (or set envs in the client’s explicit env block/CLI options). [2][3][0]

Sources
[0] Voice Mode MCP config — ".mcp.json files: environment-variable substitution is NOT supported" (example showing expansion does not work).
[1] OpenAI / Codex MCP docs — MCP config fields and env handling (shows env/env_vars and usage).
[2] Jetify / Testpilot MCP docs — examples using ${VAR}, ${VAR:-default} and $VAR.
[3] GenAIScript / Microsoft doc — lists both ${env:VAR} and ${VAR} and gives examples of interpolation.
[4] Ona / Gitpod-style docs — shows ${file:...}, ${exec:...}, ${secret:...} usage for secrets/files.


🏁 Script executed:

# Check the full context of the .cursor/mcp.json file
cat -n .cursor/mcp.json

Repository: zucky2021/ai-chatbot-api

Length of output: 1021


🏁 Script executed:

# Read the review guidelines to find prefix format
cat -n docs/development/coding-rules/review.md | head -50

Repository: zucky2021/ai-chatbot-api

Length of output: 432


want: MCP環境変数の参照構文を${env:APIDOG_ACCESS_TOKEN}に戻してください。

環境変数の参照が${env:APIDOG_ACCESS_TOKEN}から${APIDOG_ACCESS_TOKEN}に変更されています。MCP設定では${env:VAR}の明示的な形式が推奨されており、短縮形${VAR}の対応状況はクライアント実装によって異なります。Cursor IDEやClaude Desktopなど利用するMCPクライアントで確実に動作させるため、元の明示的な構文${env:APIDOG_ACCESS_TOKEN}の使用をお勧めします。

🤖 Prompt for AI Agents
.cursor/mcp.json around line 22: the MCP environment variable reference was
changed from the explicit form to a short form ("${APIDOG_ACCESS_TOKEN}");
revert it to the explicit MCP syntax "${env:APIDOG_ACCESS_TOKEN}" so clients
that require the explicit namespace (Cursor IDE, Claude Desktop, etc.) will
resolve the variable reliably—update the value to use
"${env:APIDOG_ACCESS_TOKEN}" exactly.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
.cursor/mcp.json (1)

20-20: want: --project= フラグ構文は正しいため、プロジェクトIDのハードコーディングについて確認してください。

--project= フラグ構文は apidog-mcp-server の正しい形式です。ただし、プロジェクトID 1134437 がハードコーディングされているため、異なる環境やプロジェクト間での柔軟性が失われています。

環境変数で値を管理する必要がある場合は、.env ファイル(envFile で指定済み)を使用して APIDOG_PROJECT_ID を設定し、--project=${env:APIDOG_PROJECT_ID} に変更することを検討してください。プロジェクト固有の設定として意図的なものであれば問題ありません。

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dbc30a1 and ecf8b51.

📒 Files selected for processing (1)
  • .cursor/mcp.json
🔇 Additional comments (1)
.cursor/mcp.json (1)

18-18: want: サーバー名の変更は他の設定ファイルに影響しません。

MCP サーバーのエントリ名が apidog から API specification に変更されています。コードベース内を確認したところ、他の設定ファイルやドキュメント(docs/development/tools/ を除く)で古い名前 apidog を MCP サーバーの参照として使用している箇所はないため、この変更は安全です。また、envFile で参照する .env.example には必要な環境変数 APIDOG_ACCESS_TOKEN が定義されているため、設定アプローチも妥当です。

"APIDOG_ACCESS_TOKEN": "${env:APIDOG_ACCESS_TOKEN}"
}
"args": ["-y", "apidog-mcp-server@latest", "--project=1134437"],
"envFile": "${workspaceFolder}/.env"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check repository structure and .env files
echo "=== Checking .env and related files ==="
ls -la | grep -E "\.env"

echo -e "\n=== Checking .cursor/mcp.json ==="
if [ -f ".cursor/mcp.json" ]; then
    cat -n ".cursor/mcp.json" | sed -n '19,23p'
fi

echo -e "\n=== Checking .gitignore ==="
if [ -f ".gitignore" ]; then
    grep -n "\.env" .gitignore
else
    echo ".gitignore not found"
fi

echo -e "\n=== Checking .env file content ==="
if [ -f ".env" ]; then
    echo "✓ .env exists:"
    grep "APIDOG_ACCESS_TOKEN" .env || echo "⚠ APIDOG_ACCESS_TOKEN not found"
else
    echo "✗ .env file not found"
fi

echo -e "\n=== Checking .env.example ==="
if [ -f ".env.example" ]; then
    echo "✓ .env.example exists:"
    cat ".env.example"
else
    echo "⚠ .env.example not found"
fi

Repository: zucky2021/ai-chatbot-api

Length of output: 647


🏁 Script executed:

# Check coding rules document
if [ -f "/docs/development/coding-rules/review.md" ]; then
    echo "=== Review.md found, checking content ==="
    cat "/docs/development/coding-rules/review.md"
elif [ -f "docs/development/coding-rules/review.md" ]; then
    echo "=== Found at docs/development/coding-rules/review.md ==="
    cat "docs/development/coding-rules/review.md"
else
    echo "=== Searching for review.md ==="
    find . -name "review.md" -type f 2>/dev/null | head -5
fi

Repository: zucky2021/ai-chatbot-api

Length of output: 384


want: 本番運用に向けて.envファイルを作成し、必要な環境変数を設定してください。

.envファイルが現在存在しないため、mcp.jsonenvFile参照が機能しません。以下の対応が必要です:

  1. .envファイルを作成し、.env.exampleに基づいてAPIDOG_ACCESS_TOKENなどの必要な環境変数を設定する
  2. .gitignoreへの.env除外は既に設定済みのため、機密情報は保護されています
  3. .env.exampleのドキュメントは既に存在するため、セットアップガイドとして機能します
🤖 Prompt for AI Agents
In .cursor/mcp.json around line 21, the envFile points to
"${workspaceFolder}/.env" but .env is missing; create a .env at the repository
root by copying .env.example and fill in required variables (e.g.,
APIDOG_ACCESS_TOKEN and any other keys listed in .env.example) with production
values or placeholders, keep .env listed in .gitignore (already done) so secrets
stay out of VCS, and verify the envFile path in mcp.json remains
"${workspaceFolder}/.env" so the MCP config can load the file.

@zucky2021 zucky2021 merged commit d70089e into main Dec 24, 2025
3 of 4 checks passed
@zucky2021 zucky2021 deleted the feature/27-implement-sse branch December 24, 2025 00:43
@zucky2021 zucky2021 restored the feature/27-implement-sse branch December 24, 2025 00:43
@zucky2021 zucky2021 deleted the feature/27-implement-sse branch December 24, 2025 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement SSE

1 participant