DSPy
เชื่อมต่อโปรแกรม DSPy กับ Edison Watch โดยใช้ MCP Python SDK และ dspy.Tool.from_mcp_tool
DSPy ไม่มี remote-MCP object เฉพาะ - คุณสร้าง MCP ClientSession แบบ raw ผ่าน streamablehttp_client ของ Python SDK อย่างเป็นทางการ จากนั้นแปลงแต่ละ tool ด้วย dspy.Tool.from_mcp_tool connection URL ของคุณมี API key อยู่แล้ว ดังนั้นไม่ต้องใช้ auth header
pip install dspy mcpimport 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 เป็น connection URL ของคุณ เช่น https://mcp.edison.watch/mcp/<your-api-key>/?client=dspy
streamablehttp_client yield เป็น 3-tuple (read, write, get_session_id) - ไม่ใช่ 2-tuple ที่คุณได้จาก stdio_client ทิ้งค่าที่สามเป็น _ การคัดลอกตัวอย่าง stdio และแกะ pack สองค่าเป็นข้อผิดพลาดที่พบบ่อย MCP tools เป็น async ดังนั้นให้ขับเคลื่อนโปรแกรมด้วย react.acall(...)
ทางเลือก: header สำหรับ encrypted secrets
สำหรับ server ที่มี ความลับที่เข้ารหัสแบบ zero-knowledge ให้ส่ง headers ไปยัง streamablehttp_client:
async with streamablehttp_client(
url,
headers={"x-edison-secret-key": os.environ["EDISON_SECRET_KEY"]},
) as (read, write, _):
...
