0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

FastAPI × LangGraph/ADK × MCPでAIエージェントをAPI化してみた(初心者向けコード付き)

Posted at

💡 本記事を読むとできるようになること

  • LangGraph or ADK で作ったAIエージェントを
  • MCPサーバー経由で使えるようにして
  • FastAPI 経由で呼び出し可能なAPIとして外部提供!

自然言語でリクエストを投げると、AIエージェントが裏で処理して結果を返してくれる仕組みを構築できます。

はじめに

最近、LangGraphやGoogleのAgent Development Kit(ADK)を触ってましたが、MCPサーバーとの連携例がなかなかなくて動かし方にめっちゃ苦労しました。

そこで本記事では、以下の構成で「自然言語で指示を受けて処理するAIエージェントAPI」をゼロから立ち上げられるコードを紹介します!

🔧 使ってる技術

  • Python 3.12+
  • LangGraph(LangChain製のエージェント実行フレームワーク)
  • Google Agent Development Kit(ADK)
  • MCP(Model Context Protocol)サーバー
  • FastAPI(エージェントをAPI化)

📁 構成とリポジトリ

以下のリポジトリに、各構成要素をディレクトリ単位で整理しています。
詳細はGithubをご参考ください。

📦 GitHub: Kewton/adk-mcp-starter

├── sample-mcp-server # MCPサーバー本体
├── sample-adk-agent # ADKエージェント(* 今回は使用しない)
├── sample-langGraph-agent # LangGraphエージェント(* 今回は使用しない)
└── sample-fast-api # FastAPIラッパー

🚀 セットアップ手順(超ざっくり)

  1. リポジトリをクローン

    git clone https://github.com/Kewton/adk-mcp-starter.git
    cd adk-mcp-starter
    
  2. 各モジュールのセットアップ
    mcp-server
    ターミナルを起動して下記コマンドを実行します。

    cd sample-mcp-server
    python3 -m venv venv
    source venv/bin/activate
    
    pip install --upgrade pip
    pip install -r requirements.txt
    

    ADK-AgentとLangGraph-AgentをラッピングするFastAPI
    ターミナルを起動して下記コマンドを実行します。

    cd sample-fast-api
    python3 -m venv venv
    source venv/bin/activate
    
    pip install --upgrade pip
    pip install -r requirements.txt
    
  3. .env ファイルを用意(APIキーやSSE URLを設定)
    例:
    sample-mcp-server/.env

    HOST=127.0.0.1
    PORT=8001
    

    sample-fast-api/.env

    OPENAI_API_KEY=<your api key>
    GOOGLE_API_KEY=<your api key>
    LOG_DIR=./log
    LOG_LEVEL=INFO
    SSE_SERVER_PARAMS_URL=http://localhost:8001/sse
    ADK_AGENT_MODEL=gemini-2.0-flash
    GRAPH_AGENT_MODEL=gpt-4o-mini
    
  4. それぞれ起動していく
    MCPサーバー

    cd sample-mcp-server
    source venv/bin/activate
    python main.py
    

    FastAPIサーバー

    cd sample-fast-api
    source venv/bin/activate
    uvicorn app.main:app --reload
    

APIを叩いてみる(curlでOK)

  1. LangGraphエージェント経由で実行
    curl -X POST "http://127.0.0.1:8000/aiagent-api/v1/aiagent/graph" \
        -H "Content-Type: application/json" \
        -d '{"user_input": "https://github.com/Kewton/adk-mcp-starter の記事をMarkdownに変換して"}'
    
  2. ADKエージェント経由で実行
    curl -X POST "http://127.0.0.1:8000/aiagent-api/v1/aiagent/adk" \
        -H "Content-Type: application/json" \
        -d '{"user_input": "https://github.com/Kewton/adk-mcp-starter の記事をMarkdownに変換して"}'
    

👀 最後に

LangGraph や ADK に興味あるけど「実行できるコードがない…」という人の参考になれば嬉しいです!
スター・フォローも励みになります🙏

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?