Content
# MyMiniAgent
## Library Features
- Custom response format (output_type)
- Call custom tool methods (annotations or property additions)
- General large model invocation method (api key, url, model)
- One-click context retrieval (all_messages eliminates the need for manual context recording)
- Use Field to write comments for custom class attributes to guide AI thinking
- It is best to place the API KEY in .env rather than hardcoding it into the code
## Notes on the New Version
1. The parameter name result_type has been changed to output_type;
2. The return value is no longer result.data but result.output, where output itself is of type BaseModel.
## Advanced Usage
### Using Dependency Data
When instantiating the Agent, specify deps_type, and during subsequent conversations, pass the corresponding type instance to the deps parameter to provide dependency data. However, it must be used in conjunction with tools and cannot be used solely as input data. A common scenario is to use it as an input parameter for the deps attribute of a tool method. Since deps_type only allows one type, for agents with multiple tool methods, each tool method may have different parameter types. In this case, one approach is to use a composite type structure to wrap all possible types or use Union.
### Specifying Tool Methods
Method 1: Use the annotation @agent.tool, formatted as @your Agent instance name.tool; (the tool method is defined before the Agent instantiation)
Method 2: Define the tools attribute when instantiating the Agent, tools=[method1, method2, method3]. (the tool method is defined after the Agent instantiation)
## Pain Points
The large model guiding the agent to call tool methods is not flexible enough, requiring users to manually provide the deps parameter as the tool method's parameter each time, rather than allowing the large model to autonomously extract key information from user text input to automatically fill in the tool method's parameters.
Solution: Perform a secondary encapsulation of the agent (AI guiding AI). Specifically, one AI analyzes the user input and provides formatted output as the basis for the second AI's deps generator method, and then the second AI calls the tool method. Another approach is to use a single AI, but this would result in two rounds of dialogue for a single user input: the first round for instruction orchestration to generate the corresponding deps instance, and the second round for the actual service call. However, this two-round dialogue is fixed and can only execute one operation before ending, which is insufficient for scenarios where the user input may require continuous operations (multi-round operations can only be hardcoded into the code, which is very inflexible).
Regular expression parsing has high requirements for input text and is not considered.
In fact, if all tools are local tool methods, there is no need for two agents; a single instruction orchestrator would suffice, with the remaining tasks automated by program logic. Similar to function_calling, the model is only responsible for making requests, while the actual execution of methods is handled by local logic.
## Agent Calling MCP Service Written with Pydentic_AI
Directly writing tool methods is suitable for local use, but in many cases, external tools are involved, so it is necessary to call the MCP service. Here, we use the agent implemented with the pydentic_ai library in conjunction with the official MCP library to implement the MCP service call. More detailed information can be found in the [official documentation](https://ai.pydantic.dev/mcp/client/).
There are various ways to implement the MCP client, such as http, sse, and stdio; here we will only introduce the simplest http method.
MCP server implementation:
```python
from mcp.server.fastmcp import FastMCP
app = FastMCP(
name="ben-mcp", # Seems to have no effect
port=9800, # Default is 8000
streamable_http_path="/dodo" # Default is /mcp, the path in the URL
)
@app.tool()
def get_string() -> str:
return "I am a student at Harbin Institute of Technology, my name is Zhang San, and my student ID is 123456."
if __name__ == '__main__':
app.run(transport='streamable-http') # Use streamable http method
```
MCP client implementation:
```python
from pydantic_ai import Agent
from pydantic_ai.mcp import MCPServerStreamableHTTP
from pydantic_ai.models.openai import OpenAIModel
from pydantic_ai.providers.openai import OpenAIProvider
import os
from dotenv import load_dotenv
import asyncio
load_dotenv()
deepseek_provider = OpenAIProvider(api_key=os.getenv("DEEPSEEK_API_KEY"),
base_url="https://api.deepseek.com/v1")
deepseek_model = OpenAIModel(
model_name="deepseek-chat",
provider=deepseek_provider
)
server = MCPServerStreamableHTTP('http://127.0.0.1:9800/dodo')
# Build the agent
agent = Agent(
model=deepseek_model,
system_prompt="You are an intelligent assistant",
toolsets=[server] # MCP service
)
async def main():
async with agent:
result = await agent.run('Call tool to get information')
print(result.output)
if __name__ == "__main__":
asyncio.run(main())
```
Add three lines of code at the client program entry to visualize the dialogue, and authenticate with `logfire auth` beforehand.
```python
import logfire
logfire.configure()
logfire.instrument_pydantic_ai()
```
## Handwritten RAG Personal Knowledge Base
Refer to Google's official embedding model: [gemini-api](https://ai.google.dev/gemini-api/docs/embeddings)
See the code in chunking.py and rag_agent.py.
## Frontend Setup
Utilize the AG_UI protocol (Agent User Interaction Protocol) to define the backend interface, receiving post requests and returning streaming responses.
Run:
> uvicorn agui_agent:app --reload --port 8000
Adding the reload parameter allows for automatic reloading, so you don't need to restart after modifying the code.
## Notes
- Remember to create a .env file in the demo directory and write your API key;
- If using a personal knowledge base, first set it up using embedding.py; Google's genai requires a scientific internet connection;
## A2A
In A2A, there are two important roles: one is the agent acting as the A2A client, and the other is the agent acting as the A2A server. They communicate through the A2A protocol, which standardizes communication between agents.
Pydentic_ai documentation (directly constructing A2A server): [pydentic_ai A2A](https://ai.pydantic.org.cn/a2a/)
Official website: [a2aprotocol.org](https://www.a2aprotocol.org/zh)
Getting started with A2A (official): [start A2A](https://www.a2aprotocol.org/zh/tutorials/getting-started)
Implementing A2A yourself (official): [official A2A implementation tutorial](https://www.a2aprotocol.org/zh/tutorials/implementing-a2a-in-your-application)
Enthusiast website (including Python SDK tutorials, more intuitive than the official): [a2aprotocol.net](https://www.a2aprotocol.net/zh)
A2A Python SDK (unofficial, summary version): [net](https://www.a2aprotocol.net/zh/docs/a2a-python-sdk-basic)
A2A Python SDK (official): [org](https://a2a-protocol.org/latest/tutorials/python/1-introduction/)
Connection Info
You Might Also Like
awesome-mcp-servers
A collection of MCP servers.
cc-switch
All-in-One Assistant for Claude Code, Codex & Gemini CLI across platforms.
git
A Model Context Protocol server for Git automation and interaction.
oh-my-opencode
Background agents · Curated agents like oracle, librarians, frontend...
TrendRadar
TrendRadar: Your hotspot assistant for real news in just 30 seconds.
Appwrite
Build like a team of hundreds