You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/content/docs/development/tool_use.md
+75-70Lines changed: 75 additions & 70 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,15 +28,15 @@ Flink Agents provides a flexible and extensible tool use mechanism. Developers c
28
28
29
29
## Local Function as Tool
30
30
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.
31
+
Developer can define the tool as a local Python function, and there are two way to define and register an local function as a tool:
34
32
35
33
{{< hint info >}}
36
34
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.
37
35
{{< /hint >}}
38
36
39
-
Below is an example of how to define the tool as a local Python function in workflow agent:
37
+
### Define Tool as Static Method in Agent Class
38
+
39
+
Developer can define the tool as a static method in the agent class while defining the workflow agent, and use the `@tool` annotation to mark the method as a tool. The tool can be referenced by its name in the `tools` list of the `ResourceDescriptor` when creating the chat model in the agent.
40
40
41
41
```python
42
42
classReviewAnalysisAgent(Agent):
@@ -55,11 +55,28 @@ class ReviewAnalysisAgent(Agent):
55
55
The negative review content
56
56
"""
57
57
notify_shipping_manager(id=id, review=review)
58
+
59
+
@chat_model_setup
60
+
@staticmethod
61
+
defreview_analysis_model() -> ResourceDescriptor:
62
+
"""ChatModel which focus on review analysis."""
63
+
return ResourceDescriptor(
64
+
clazz=OllamaChatModelSetup,
65
+
...,
66
+
tools=["notify_shipping_manager"], # reference the tool by its name
67
+
)
58
68
59
69
...
60
70
```
61
71
62
-
Below is an example of how to define the tool as a local Python function in react agent:
72
+
**Key points:**
73
+
- Use `@tool` decorator to define the tool
74
+
- Reference the tool by its name in the `tools` list of the `ResourceDescriptor`
75
+
76
+
77
+
### Register Tool to Execution Environment
78
+
79
+
Developer can register the tool to the execution environment, and then reference the tool by its name. This allows the tool to be reused by multiple agents.
tools=["notify_shipping_manager"],# reference the tool by its name
92
109
),
93
110
...
94
111
)
95
112
```
96
113
97
-
## Integrate with MCP Server
114
+
**Key points:**
115
+
- Use `AgentsExecutionEnvironment.add_resource` to register the tool to the execution environment
116
+
- Reference the tool by its name in the `tools` list of the `ResourceDescriptor`
98
117
99
-
Flink Agents supports integrating with a remote MCP server to use the resources provided by the MCP server, including tools and prompts.
118
+
## MCP Tool
100
119
101
-
To use MCP server in workflow agent, developer can use `@mcp_server` annotation to declare the server.
120
+
{{< hint info >}}
121
+
MCP (Model Context Protocol) is a standardized protocol for integrating AI applications with external data sources and tools. MCP tools allow dynamic tool retrieval from MCP servers.
122
+
{{< /hint >}}
102
123
103
-
```python
104
-
@mcp_server
105
-
@staticmethod
106
-
defmy_mcp_server() -> MCPServer:
107
-
"""Define MCP server connection."""
108
-
return MCPServer(endpoint=MCP_SERVER_ENDPOINT)
109
-
```
124
+
MCP tools are managed by external MCP servers and automatically discovered when you define an MCP server connection in your agent.
110
125
111
-
To use MCP server in react agent, developer can register the MCP server to the execution environment.
126
+
### Define MCP Server with Tools
112
127
113
-
```python
114
-
# Register MCP server to the execution environment.
"""Notify the shipping manager when product received a negative review due to
137
+
shipping damage.
121
138
122
-
If developer define a MCP server providing tool `add` and prompt `ask_sum`, they can use them when talking with chat model.
139
+
Parameters
140
+
----------
141
+
id : str
142
+
The id of the product that received a negative review due to shipping damage
143
+
review: str
144
+
The negative review content
145
+
"""
146
+
...
123
147
124
-
```python
125
-
@chat_model_setup
126
-
@staticmethod
127
-
defmath_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
-
)
148
+
mcp.run("streamable-http")
137
149
```
138
150
139
-
## Built-in Events for Tool
151
+
**Key points:**
152
+
- Use `@mcp.tool()` decorator to define tools
153
+
- The function name becomes the tool identifier
140
154
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.
155
+
### Use MCP Tools in Agent
142
156
143
-
Here is the definition of the `ToolRequestEvent` and `ToolResponseEvent`:
157
+
Connect to the MCP server and use its tools in your agent:
144
158
145
159
```python
146
-
classToolRequestEvent(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
-
classToolResponseEvent(Event):
162
-
"""Event representing a result from tool call.
160
+
classReviewAnalysisAgent(Agent):
161
+
...
163
162
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
0 commit comments