> your AI agent picks dependencies from memory; give it dated facts — try starlog.dev ↗ vet your agent's deps ↗ vibe-coding is fine. vibe-importing isn’t. — try starlog.dev ↗ vibe-importing isn’t fine ↗ your agent has never seen your private packages — try starlog.dev ↗ facts for private packages ↗ a linter for the dependencies your AI agent picks — try starlog.dev ↗ a linter for agent deps ↗

Back to Articles

Agenspy: Protocol-First Architecture Brings Modern Agent Communication to DSPy

[ View on GitHub ]

Agenspy: Protocol-First Architecture Brings Modern Agent Communication to DSPy

Hook

While everyone rushed to add MCP support to their AI frameworks, DSPy users were stuck converting protocol tools back into DSPy.Tools—a manual process that defeated the entire purpose of having standardized agent protocols.

Context

DSPy emerged as a framework for optimizing language model prompts through a module-based approach, treating prompts as code rather than strings. Its strength lies in systematic optimization through techniques like bootstrapping and few-shot learning. However, as the AI agent ecosystem evolved toward standardized communication protocols—particularly Anthropic's Model Context Protocol (MCP) and Google's Agent2Agent (A2A)—DSPy found itself architecturally misaligned.

The problem wasn't that DSPy couldn't work with these protocols. Rather, its tool-first design meant developers had to manually wrap MCP resources as DSPy.Tools, creating friction exactly where protocols promised to reduce it. When Google announced plans for an A2A Agent Directory, the DSPy community faced a decision: continue retrofitting protocol tools one-by-one, or rethink the architecture from the ground up. Agenspy emerged from DSPy issue #8273 as an answer—a protocol-first layer that lets DSPy agents speak MCP and A2A natively while preserving DSPy's optimization capabilities.

Technical Insight

Agenspy implements a three-layer architecture that sits atop DSPy's core modules. The bottom layer consists of protocol implementations (MCP, Agent2Agent, with slots for future protocols). The middle layer provides protocol abstraction—managing connections, capability negotiation, and session state. The top layer integrates with DSPy's ChainOfThought, Predict, and ReAct modules, exposing protocol capabilities as if they were native DSPy operations.

The elegance appears in how Agenspy handles protocol connections. Instead of requiring developers to manually instantiate MCP servers and wire them to agents, the framework provides declarative configuration:

from agenspy import MCPAgent
import dspy

# Configure DSPy with your LLM
dspy.configure(lm=dspy.LM(model="gpt-4"))

# Create an agent with MCP server connections
agent = MCPAgent(
    mcp_servers={
        "github": {
            "command": "npx",
            "args": ["-y", "@modelcontextprotocol/server-github"],
            "env": {"GITHUB_TOKEN": "your_token"}
        },
        "filesystem": {
            "command": "python",
            "args": ["-m", "mcp_server_filesystem", "/workspace"]
        }
    },
    tools=["read_file", "create_pr", "search_repos"]
)

# The agent automatically negotiates capabilities
result = await agent.execute(
    "Review the Python files in this repo and create a PR with type hints"
)

Under the hood, Agenspy manages the entire protocol lifecycle. When you instantiate an MCPAgent, it spawns MCP server processes, establishes stdio/SSE connections, performs capability negotiation, and builds a DSPy-compatible tool registry. The agent can then reason about which tools to use through DSPy's ReAct module, but the actual tool invocations route through the protocol layer—preserving MCP's streaming responses, error handling, and resource management.

The protocol abstraction becomes powerful when you need multi-protocol support. Agenspy's experimental MultiProtocolAgent can coordinate between MCP and Agent2Agent simultaneously:

from agenspy import MultiProtocolAgent

multi_agent = MultiProtocolAgent(
    protocols=[
        {"type": "mcp", "servers": mcp_config},
        {"type": "a2a", "directory": "https://agent-directory.example.com"}
    ],
    routing_strategy="capability_match"  # Route tasks to best protocol
)

# Agent decides whether to use local MCP tools or delegate to A2A agents
result = await multi_agent.execute(
    "Analyze this codebase and generate a security report"
)

The routing strategy examines the task, checks available capabilities across all connected protocols, and selects the optimal execution path. An MCP server might handle file operations while an A2A security analysis agent processes the actual review—all orchestrated through DSPy's reasoning modules.

For teams needing custom protocol implementations, Agenspy exposes a BaseAgent class and protocol interface. You can implement support for proprietary agent protocols while maintaining compatibility with the DSPy optimization pipeline. The async-first design ensures protocol I/O doesn't block the reasoning loop, critical for agents that need to maintain multiple concurrent protocol sessions.

Gotcha

Agenspy's protocol abstraction introduces complexity that not every project needs. If you're building a simple agent that calls three specific APIs, wrapping them as DSPy.Tools remains simpler than setting up MCP servers and protocol configuration. The protocol layer adds debugging surface area—now you're troubleshooting not just your agent logic and DSPy modules, but also protocol handshakes, capability mismatches, and inter-process communication.

The multi-protocol support is explicitly marked experimental, and for good reason. Routing between protocols requires the agent to reason about capability semantics across different protocol specifications—a hard problem when MCP and A2A don't share identical capability models. The project is also young (77 stars) with documentation that cuts off mid-example in places, suggesting active development. Production deployments should stick with single-protocol configurations until the multi-protocol abstraction stabilizes. Additionally, you'll need familiarity with both DSPy's module system and the protocol specifications you're targeting. The learning curve is steeper than monolithic frameworks that hide protocol details entirely.

Verdict

Use if: You're already invested in DSPy and need your agents to communicate via MCP or Agent2Agent protocols, you're building agents that should interoperate with Google's A2A Agent Directory or Anthropic's MCP ecosystem, you want to future-proof against emerging agent communication standards, or you need the flexibility to switch between protocol implementations without rewriting agent logic. Skip if: You're not using DSPy (LangChain and AutoGen have more mature protocol support), you need rock-solid multi-protocol routing in production today (wait 6-12 months), your use case is simple enough that direct API calls or DSPy.Tools suffice, or you prefer frameworks that abstract protocols entirely rather than exposing them as first-class concepts. Agenspy fills a specific architectural gap for DSPy practitioners, but it's a power tool for a particular problem space.