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
@@ -22,33 +22,157 @@ specific language governing permissions and limitations
22
22
under the License.
23
23
-->
24
24
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.
25
28
26
29
## Local Function as Tool
27
30
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.
30
37
{{< /hint >}}
31
38
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:
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
+
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
+
)
137
+
```
49
138
50
139
## Built-in Events for Tool
51
140
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
+
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.
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
0 commit comments