The Model Context Protocol (MCP) is emerging as a standardized way for AI agents to interact with external tools and services. By creating MCP servers, developers can expose functionalities that AI agents can discover and utilize, fostering a more interoperable and powerful ecosystem of AI applications. While many implementations lean on the popular pydantic library for data validation and settings management, building robust MCP servers and agentic workflows in Python without it is entirely feasible. This approach can offer benefits such as reduced dependencies, greater control over data handling, and potentially faster performance in certain scenarios.
Here, we’ll explore several ways to leverage MCP servers for AI agents and agentic workflows using standard Python and other lightweight libraries.
Core Concepts: MCP
At its heart, MCP is a contract-based protocol. An MCP server exposes a set of tools (callable functions) and resources (retrievable data) to an MCP client (the AI agent). The communication between them relies on a structured format, typically JSON. The primary challenge in a pydantic-free implementation is to define and enforce this structure for data validation and serialization.
Instead of pydantic, we can turn to:
dataclasses: Part of the Python standard library since version 3.7,dataclassesprovide a concise way to create classes primarily used for storing data. They can be used to define the expected structure of tool arguments and resource formats.typingmodule: Python’s built-intypingmodule allows for type hinting, which, while not enforcing types at runtime by default, serves as excellent documentation and can be used by other validation libraries.- Manual Dictionary Validation: For simpler cases, you can write your own validation functions that check the keys and value types of incoming JSON data (represented as Python dictionaries).
- Lightweight Validation Libraries: Libraries like
cerberusorattrs(which can be seen as a precursor todataclasseswith more features) can provide more powerful validation capabilities without the full feature set and potential overhead ofpydantic.
Brainstorming Use Cases and Implementation Patterns
Here are several ways to use MCP servers for AI agents and agentic workflows in a pydantic-free Python environment:
1. The Sandboxed Code Executioner Agent
An incredibly useful tool for an AI agent is the ability to execute code in a secure environment. An MCP server can provide this functionality.
-
MCP Server Implementation:
-
Tool Definition: The server would expose a
run_python_codetool. The input to this tool could be defined using adataclass: -
Server Logic: The server would receive a JSON request, validate that it contains the
codekey (and optionallytimeout), and then execute the code in a sandboxed environment (e.g., usingexecwithin a restricted scope or a more secure solution like a Docker container). The output or any errors would be returned as a JSON response. -
Data Handling: A simple function can validate the incoming dictionary to ensure it matches the
CodeExecutionRequeststructure before proceeding.
-
-
AI Agent and Workflow:
- The AI agent, when tasked with a problem that requires computation or interaction with a library it doesn’t have direct access to, can discover and use the
run_python_codetool from the MCP server. - Agentic Workflow:
- User Prompt: ‘Calculate the 20th Fibonacci number and tell me if it’s a prime number.’
- Agent’s Thought Process: The agent identifies the need for both calculation and primality testing.
- Tool Use: The agent formulates Python code to perform these tasks and sends it to the MCP server via the
run_python_codetool. - Response and Action: The server executes the code and returns the result. The agent then uses this result to answer the user’s question.
- The AI agent, when tasked with a problem that requires computation or interaction with a library it doesn’t have direct access to, can discover and use the
2. The Multi-API Orchestrator Agent
Agents often need to interact with multiple external APIs to accomplish a task. An MCP server can act as a unified gateway to these APIs, simplifying the agent’s logic.
-
MCP Server Implementation:
- Tool Definitions: The server would expose tools that correspond to various API endpoints. For example, a
get_weather(city: str)tool and aget_stock_price(ticker: str)tool. The arguments for each tool can be defined usingdataclasses. - Server Logic: The server would handle the authentication and request logic for each underlying API. This encapsulates the complexity away from the agent.
- Data Handling: The server would validate the incoming tool requests and format the responses from the external APIs into a consistent structure.
- Tool Definitions: The server would expose tools that correspond to various API endpoints. For example, a
-
AI Agent and Workflow:
- User Prompt: ‘What’s the weather in New York and the current stock price of Google?’
- Agent’s Thought Process: The agent recognizes the need for two different pieces of information from two separate sources.
- Tool Use: The agent discovers the
get_weatherandget_stock_pricetools on the MCP server and calls them with the appropriate arguments. - Response and Synthesis: The agent receives the weather data and stock price from the server and synthesizes them into a single, coherent response for the user.
3. The Stateful Long-Term Project Assistant
For complex, multi-step tasks, an agent needs to maintain state. An MCP server can be designed to manage this state.
-
MCP Server Implementation:
- Resource and Tool Definitions:
- A
projectsresource that can be queried to get a list of ongoing projects. - A
create_project(name: str)tool to start a new project. - A
add_to_project(project_name: str, data: dict)tool to add information to an existing project.
- A
- Server Logic: The server would maintain a persistent storage (e.g., a simple file-based database like SQLite or even JSON files) to store the state of each project.
- Data Handling: Using
dataclassesand manual validation to ensure the structure of project data is consistent.
- Resource and Tool Definitions:
-
AI Agent and Workflow:
- User Interaction (over multiple conversations):
- ‘Let’s start a new project called ‘Vacation Planning’.’ (Agent uses
create_project) - ‘Find flights to Honolulu for next month.’ (Agent uses an external tool and then
add_to_projectto store the findings) - ‘What have we planned for the vacation so far?’ (Agent queries the
projectsresource on the MCP server)
- ‘Let’s start a new project called ‘Vacation Planning’.’ (Agent uses
- Agentic Workflow: This creates a powerful, long-running workflow where the agent can pick up a task where it left off, thanks to the stateful nature of the MCP server.
- User Interaction (over multiple conversations):
4. The Collaborative Multi-Agent System
Multiple specialized AI agents can collaborate on a complex problem by sharing information through a central MCP server that acts as a message bus or a shared blackboard.
-
MCP Server Implementation:
- Resource and Tool Definitions:
- A
messagesresource to fetch the latest messages or updates. - A
post_message(agent_id: str, content: dict)tool for an agent to broadcast information.
- A
- Server Logic: The server would store messages with timestamps and agent identifiers, allowing agents to subscribe to or poll for updates.
- Resource and Tool Definitions:
-
AI Agent and Workflow:
- Scenario: A research task involving a ‘Researcher’ agent and a ‘Writer’ agent.
- Workflow:
- The user asks the ‘Researcher’ to gather information on a topic.
- The ‘Researcher’ agent finds relevant articles and posts summaries to the MCP server using
post_message. - The ‘Writer’ agent, which periodically checks the
messagesresource, sees the new information from the ‘Researcher’. - The ‘Writer’ agent then uses these summaries to draft a report, which it might also post back to the server for the user or another agent to review.
Conclusion
Building MCP servers for AI agents and agentic workflows in Python without pydantic is a viable and potentially advantageous approach. By leveraging Python’s standard library features like dataclasses and the typing module, or incorporating lightweight validation libraries, developers can create powerful, efficient, and dependency-lean AI systems. The key is to maintain a clear and consistent contract between the MCP server and its clients, ensuring that the structured data exchange at the heart of the protocol is handled robustly. This opens the door to a wide range of innovative and powerful AI applications.
Here are several ways to use MCP Servers for AI agents and agentic workflows in Python without using the pydantic library.
1. Leverage Standard Library Dataclasses for Data Contracts
You can use Python’s built-in dataclasses to define the structure of the data your MCP server’s tools and resources will handle. This approach provides type hinting and some basic structure without external dependencies.
MCP Server Tool Definition (server.py):
AI Agent (Client-side) Interaction:
Your AI agent would be responsible for generating a JSON string that conforms to the CreateNoteInput structure and then parsing the JSON response.
2. Utilize TypedDict for Lighter-weight Structures
For an even more lightweight approach, you can use typing.TypedDict. This is particularly useful when you’re primarily working with dictionary-like data, which is common in API interactions.
MCP Server Tool Definition with TypedDict (server.py):
AI Agent (Client-side) Logic:
The AI agent would need to understand that it needs to provide a user_id and can expect a JSON object back that will have the keys defined in UserProfile.
3. Implement Manual Validation with Dictionaries
The most basic approach is to use standard Python dictionaries and perform manual validation. This gives you maximum control but requires more boilerplate code.
MCP Server Tool with Manual Validation (server.py):
4. Agentic Workflows with MCP Servers
With your pydantic-free MCP server running, you can create agentic workflows that chain calls to the server’s tools and resources.
Example Agentic Workflow:
Imagine a workflow to onboard a new user.
- Agent 1 (User Interaction Agent): Gathers the user’s name and email from a chat.
- Agent 1 calls the MCP Server’s
create_usertool:- Input to tool:
{"name": "John Doe", "email": "john.doe@example.com"} - Output from tool:
{"user_id": 456, "status": "created"}
- Input to tool:
- Agent 1 receives the
user_idand triggers Agent 2. - Agent 2 (Onboarding Agent): Is tasked with creating a welcome document for the new user.
- Agent 2 calls the MCP Server’s
create_documenttool:- Input to tool:
{"user_id": 456, "title": "Welcome, John Doe!", "content": "Here are some getting started tips..."} - Output from tool:
{"document_id": "doc-abc-123", "status": "success"}
- Input to tool:
- The workflow completes.
This workflow demonstrates how different agents can collaborate by passing information obtained from an MCP server, all without relying on pydantic for data structuring. The key is a clear and agreed-upon data format (like JSON schemas described in your tool’s documentation) that the agents and the MCP server adhere to.