Skip to content

Commit 03dac4f

Browse files
committed
[docs] Update the tool use docs
1 parent bd2f2f6 commit 03dac4f

File tree

1 file changed

+141
-17
lines changed

1 file changed

+141
-17
lines changed

docs/content/docs/development/tool_use.md

Lines changed: 141 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,157 @@ specific language governing permissions and limitations
2222
under the License.
2323
-->
2424

25+
## Overview
26+
27+
Flink Agents provides a flexible and extensible tool use mechanism. Developers can define the tool as a local Python function, or they can integrate with a remote MCP server to use the tools provided by the MCP server.
2528

2629
## Local Function as Tool
2730

28-
{{< hint warning >}}
29-
**TODO**: How to define and use a Local Function as Tool.
31+
Developer can define the tool as a local Python function, and use it in either workflow agent or react agent.
32+
- For workflow agent, developer defines the tool as a static method in the agent class, and use the `@tool` annotation to mark the method as a tool.
33+
- For react agent, you have to register the tool to the execution environment, and then pass the tool name to the chat model descriptor when creating the ReAct agent.
34+
35+
{{< hint info >}}
36+
Flink Agents uses the docstring of the tool function to generate the tool metadata. The docstring of the python function should accurately describe the tool's purpose, parameters, and return value, so that the LLM can understand the tool and use it effectively.
3037
{{< /hint >}}
3138

32-
## How to Integrate with MCP Server
39+
Below is an example of how to define the tool as a local Python function in workflow agent:
3340

34-
{{< hint warning >}}
35-
**TODO**: How to integrate with MCP Server.
36-
{{< /hint >}}
41+
```python
42+
class ReviewAnalysisAgent(Agent):
3743

38-
### MCP Tools
44+
@tool
45+
@staticmethod
46+
def notify_shipping_manager(id: str, review: str) -> None:
47+
"""Notify the shipping manager when product received a negative review due to
48+
shipping damage.
3949
40-
{{< hint warning >}}
41-
**TODO**: How to use the tools provided by MCP Server.
42-
{{< /hint >}}
50+
Parameters
51+
----------
52+
id : str
53+
The id of the product that received a negative review due to shipping damage
54+
review: str
55+
The negative review content
56+
"""
57+
notify_shipping_manager(id=id, review=review)
58+
59+
...
60+
```
4361

44-
### MCP Prompt
62+
Below is an example of how to define the tool as a local Python function in react agent:
4563

46-
{{< hint warning >}}
47-
**TODO**: How to use the prompts provided by MCP Server.
48-
{{< /hint >}}
64+
```python
65+
def notify_shipping_manager(id: str, review: str) -> None:
66+
"""Notify the shipping manager when product received a negative review due to
67+
shipping damage.
68+
69+
Parameters
70+
----------
71+
id : str
72+
The id of the product that received a negative review due to shipping damage
73+
review: str
74+
The negative review content
75+
"""
76+
...
77+
78+
...
79+
80+
# Add notify shipping manager tool to the execution environment.
81+
agents_env.add_resource(
82+
"notify_shipping_manager", Tool.from_callable(notify_shipping_manager)
83+
)
84+
85+
...
86+
87+
# Create react agent with notify shipping manager tool.
88+
review_analysis_react_agent = ReActAgent(
89+
chat_model=ResourceDescriptor(
90+
clazz=OllamaChatModelSetup,
91+
tools=["notify_shipping_manager"],
92+
),
93+
...
94+
)
95+
```
96+
97+
## Integrate with MCP Server
98+
99+
Flink Agents supports integrating with a remote MCP server to use the resources provided by the MCP server, including tools and prompts.
100+
101+
To use MCP server in workflow agent, developer can use `@mcp_server` annotation to declare the server.
102+
103+
```python
104+
@mcp_server
105+
@staticmethod
106+
def my_mcp_server() -> MCPServer:
107+
"""Define MCP server connection."""
108+
return MCPServer(endpoint=MCP_SERVER_ENDPOINT)
109+
```
110+
111+
To use MCP server in react agent, developer can register the MCP server to the execution environment.
112+
113+
```python
114+
# Register MCP server to the execution environment.
115+
agents_env.add_resource("my_mcp_server", MCPServer(endpoint=MCP_SERVER_ENDPOINT))
116+
```
117+
118+
### Use MCP Tool and MCP Prompt
119+
120+
The MCP tool and prompt can be used the in same way with local function tool and local prompt.
121+
122+
If developer define a MCP server providing tool `add` and prompt `ask_sum`, they can use them when talking with chat model.
123+
124+
```python
125+
@chat_model_setup
126+
@staticmethod
127+
def math_chat_model() -> ResourceDescriptor:
128+
"""ChatModel using MCP prompt and tool."""
129+
return ResourceDescriptor(
130+
clazz=OllamaChatModelSetup,
131+
connection="ollama_connection",
132+
model=OLLAMA_MODEL,
133+
prompt="ask_sum", # MCP prompt registered from my_mcp_server
134+
tools=["add"], # MCP tool registered from my_mcp_server
135+
extract_reasoning=True,
136+
)
137+
```
49138

50139
## Built-in Events for Tool
51140

52-
{{< hint warning >}}
53-
**TODO**: How to use the built-in events for tool call request and tool call response, such as ToolRequestEvent and ToolResponseEvent.
54-
{{< /hint >}}
141+
Flink Agents provides built-in events for tool call request and tool call response, specifically `ToolRequestEvent` and `ToolResponseEvent`. By default, Flink Agents built-in action will listen to these events and handle the tool call request and tool call response automatically. If you have special needs, you can also define your own action to listen to these events and handle the `ToolRequestEvent` and `ToolResponseEvent` accordingly.
142+
143+
Here is the definition of the `ToolRequestEvent` and `ToolResponseEvent`:
144+
145+
```python
146+
class ToolRequestEvent(Event):
147+
"""Event representing a tool call request.
148+
149+
Attributes:
150+
----------
151+
model: str
152+
name of the model that generated the tool request.
153+
tool_calls : List[Dict[str, Any]]
154+
tool calls that should be executed in batch.
155+
"""
156+
157+
model: str
158+
tool_calls: List[Dict[str, Any]]
159+
160+
161+
class ToolResponseEvent(Event):
162+
"""Event representing a result from tool call.
163+
164+
Attributes:
165+
----------
166+
request_id : UUID
167+
The id of the request event.
168+
responses : Dict[UUID, Any]
169+
The dict maps tool call id to result.
170+
external_ids : Dict[UUID, str]
171+
Optional identifier for storing original tool call IDs from external systems
172+
(e.g., Anthropic tool_use_id).
173+
"""
174+
175+
request_id: UUID
176+
responses: Dict[UUID, Any]
177+
external_ids: Dict[UUID, str | None]
178+
```

0 commit comments

Comments
 (0)