Edison Watch
開発者

DSPy

DSPyプログラムをMCP Python SDKとdspy.Tool.from_mcp_toolを使ってEdison Watchに接続する。

DSPy にはリモートMCP専用のオブジェクトが含まれていません。公式Python SDKの streamablehttp_client を使って生のMCP ClientSession を立ち上げ、各ツールを dspy.Tool.from_mcp_tool で変換します。接続URL にはAPIキーが含まれているため、認証ヘッダーは不要です。

pip install dspy mcp
import asyncio
import os

import dspy
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client

dspy.configure(lm=dspy.LM("openai/gpt-5.6-luna"))

class Assistant(dspy.Signature):
    """Answer the user request using the available tools."""
    request: str = dspy.InputField()
    answer: str = dspy.OutputField()

async def main() -> None:
    url = os.environ["EDISON_MCP_URL"]
    async with streamablehttp_client(url) as (read, write, _):
        async with ClientSession(read, write) as session:
            await session.initialize()
            tools = await session.list_tools()
            dspy_tools = [dspy.Tool.from_mcp_tool(session, t) for t in tools.tools]

            react = dspy.ReAct(Assistant, tools=dspy_tools)
            result = await react.acall(request="List my available tools.")
            print(result.answer)

asyncio.run(main())

EDISON_MCP_URL に接続URLを設定してください(例:https://mcp.edison.watch/mcp/<your-api-key>/?client=dspy)。

streamablehttp_client3タプル (read, write, get_session_id) を返します。stdio_client で得られる2タプルとは異なります。3番目の値は _ として破棄してください。stdioの例をコピーして2つの値のみアンパックするのはよくある間違いです。MCPツールは非同期なので、react.acall(...) でプログラムを動かしてください。

オプション:暗号化シークレットヘッダー

ゼロ知識暗号化シークレット を使用するサーバーには、streamablehttp_clientheaders を渡します。

async with streamablehttp_client(
    url,
    headers={"x-edison-secret-key": os.environ["EDISON_SECRET_KEY"]},
) as (read, write, _):
    ...