Index of every page on this site: llms.txt
All skills
omidzamani avatar

/dspy-mcp-tool-integration

@f5db3b7
by OMID Zamaniomidzamani/dspy-skills125 stars
13
Run it yourself

Use for MCP tools with DSPy, Model Context Protocol servers, dspy.Tool.from_mcp_tool, and ReAct agents over MCP-compatible tools.

Use this Skill: https://skilld.dev/gh/omidzamani/dspy-skills/dspy-mcp-tool-integration

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

SKILL.md

≈39 tokens for metadata: the name and description. ≈666 when used: this file.

Description uses 2.0% of example budget

Before choosing a Skill, your Agent reads its name and description. All available Skills share that space.

  • A shorter description leaves more room for other Skills. This entry exceeds our 1% size suggestion.

In our Claude Code example, all Skill names and descriptions share 8,000 characters. This Skill uses ≈157 characters, or 2.0%.

The 1% threshold is a size suggestion. Longer descriptions can still fit.

Your model, settings, and other Skills decide how much text your Agent can read.

Example settings and source

The example uses a 200k-token context and default Claude Code settings. The count includes the name, description, separators, and when_to_use when present. Codex also counts local file paths.

Skit's source and limits: Codex 0.160.1, Claude Code 2.1.292.

DSPy MCP Tool Integration

Goal

Connect an MCP server with the MCP Python client, convert its tools to dspy.Tool, and use them in an async DSPy agent.

Install

pip install -U "dspy[mcp]>=3.2.1,<3.3"

DSPy converts tools but does not manage MCP connections. Keep the ClientSession alive for as long as the DSPy tools are in use.

Streamable HTTP Server

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

async def main():
    async with streamablehttp_client("http://localhost:8000/mcp") as (read, write):
        async with ClientSession(read, write) as session:
            await session.initialize()
            response = await session.list_tools()
            tools = [dspy.Tool.from_mcp_tool(session, tool) for tool in response.tools]

            agent = dspy.ReAct("task -> result", tools=tools, max_iters=5)
            output = await agent.acall(task="Check the weather in Tokyo")
            print(output.result)

asyncio.run(main())

Local Stdio Server

import asyncio
import dspy
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

async def main():
    params = StdioServerParameters(
        command="python3",
        args=["path/to/server.py"],
        env=None,
    )

    async with stdio_client(params) as (read, write):
        async with ClientSession(read, write) as session:
            await session.initialize()
            response = await session.list_tools()
            tools = [dspy.Tool.from_mcp_tool(session, tool) for tool in response.tools]
            agent = dspy.ReAct("question -> answer", tools=tools, max_iters=5)
            print((await agent.acall(question="What is 25 + 17?")).answer)

asyncio.run(main())

Best Practices

  1. Use acall() because MCP tools are asynchronous.
  2. Initialize the session before listing tools.
  3. Keep tool descriptions precise at the MCP server boundary.
  4. Apply authentication and authorization before exposing sensitive tools.
  5. Set a modest max_iters and trace tool use in production.

Related Skills

Official Documentation

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts4mo3 checks · Risk SAFE
  • Gen Agent Trust Hub4mo

    The skill provides instructions and code examples for connecting Model Context Protocol (MCP) servers to DSPy agents. It demonstrates standard library usage for both network-based and local process-based MCP tool integration. No security issues or malicious patterns were identified.

  • Socket4mo

    No alerts

  • Snyk4mo

    Risk: LOW · No issues

Signed by skilld at f5db3b7. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 5 months ago
What it can do
Reads files Edits files
version
1.0.0
dspy-compatibility
3.2.1
tags
[
  "agent",
  "production"
]
All 4 allowed tools
ReadWriteGlobGrep
Other metadata
requires-extras
[
  "dspy[mcp]"
]

README badge

README badge for omidzamani/dspy-skills/dspy-mcp-tool-integration