Content
# 🔌 a2a-protocol-demo7: Function Calling & Tool Slots System
[](https://opensource.org/licenses/MIT)
[](https://www.python.org/)
**a2a-protocol-demo7** is the seventh milestone in the Agent protocol series. Building on the foundations of "Communication (A2A)" and "Memory (Memory/Funnel)" addressed in previous chapters, this project empowers the Agent with the ability of **"hands"**.
It demonstrates how to implement a mechanism similar to OpenAI Function Calling from scratch and standardizes the outputs of tool executions into **MCP Slots**.
## 🎯 Core Objectives
1. **Plugin Architecture (Plugin Registry)**: Demonstrates how to automatically register ordinary functions as Agent tools using Python's reflection mechanism (`inspect`), generating JSON Schema.
2. **Execution Loop**: Implements the standard ReAct cycle of `Think` -> `Call` -> `Execute` -> `Observe` -> `Reply`.
3. **Slot Standardization (Slot-based Tools)**: Shows why both the **call instructions** and **execution results** of tools must be encapsulated as Slots for easier management and compression by the Context Pipeline.
## 🏗️ System Architecture
### Core Components
* **`registry.py`**: **Registry**. Responsible for managing the list of tools and generating tool description documents that LLM can understand.
* **`tools.py`**: **Capability Layer**. Defines specific business functions (e.g., checking the weather, calculating exchange rates).
* **`executor.py`**: **Brain Layer**. Simulates the decision-making process of the LLM, intercepts tool call instructions, executes code, and writes results back to memory.
* **`protocol.py`**: **Protocol Layer**. Introduces two new Slot types: `TOOL_CALL` (call request) and `TOOL_RESULT` (execution result).
### Interaction Sequence Diagram
```
sequenceDiagram
participant User
participant Agent (Executor)
participant Registry
participant Tool (Python Function)
User->>Agent: "Please check the weather in Shanghai"
rect rgb(240, 240, 250)
Note over Agent, Registry: 1. Intent Recognition (Think)
Agent->>Registry: Retrieve available tool list
Agent->>Agent: Decision: Need to call search_weather
end
rect rgb(255, 245, 235)
Note over Agent, Tool: 2. Tool Execution (Act)
Agent->>Agent: Generate Slot [TYPE=TOOL_CALL]
Agent->>Tool: Execute search_weather(city="Shanghai")
Tool-->>Agent: Returns "Sunny, 25°C"
end
rect rgb(235, 255, 240)
Note over Agent, User: 3. Result Feedback (Observe & Reply)
Agent->>Agent: Encapsulate Slot [TYPE=TOOL_RESULT]
Agent->>User: "Today in Shanghai, it's sunny with a temperature of 25 degrees..."
end
```
## 🛠️ Quick Start
### 1. Install Dependencies
```bash
pip install fastapi uvicorn requests pydantic
```
### 2. Start the Server
The server will start an A2A gateway and load two example tools: `search_weather` and `calculate_cost`.
```bash
python server.py
# Output: 🔌 Plugin System Server running on port 8000
```
### 3. Run the Client
The client simulates user commands with different intents, observing how the Agent automatically selects tools.
```bash
python client.py
```
## 📂 Code Structure Explanation
```text
a2a-protocol-demo7/
├── protocol.py # [Core] Defines the Slot types TOOL_CALL and TOOL_RESULT
├── registry.py # [Tools] Automatically converts Python functions to JSON Schema
├── tools.py # [Business] Actual implementation of Python functions
├── executor.py # [Logic] Contains the implementation of the intelligent loop (The Loop)
├── server.py # [Integration] FastAPI server
└── client.py # [Testing] Simulates user requests
```
## 🧠 Deep Thought: Why Should Tool Results Be Slots?
In a simple demo, it might seem sufficient for tools to return strings directly. However, in a complex **MCP (Model Context Protocol)** architecture, encapsulating results as `Slots` is crucial:
1. **Unified Flow**: Tool results (Slots) and user interactions (Slots) are treated equally in the pipeline.
2. **Compressibility**: If a SQL query tool returns 1MB of JSON data, the **Context Funnel (Demo 4)** can recognize this as a `TOOL_RESULT` type Slot, allowing for aggressive compression (e.g., retaining only a summary) to prevent overwhelming the LLM context.
3. **Metadata Tracking**: The `metadata` field of a Slot can store `latency`, `source`, and `status` (success/failure), which is vital for monitoring in production environments.
## 📜 License
MIT