Duibonduil commited on
Commit
f376f50
·
verified ·
1 Parent(s): 561e351

Upload 6 files

Browse files
examples/mcp_demo/README.md ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MCP demo
2
+
3
+ 1. Start example mcp server use the command:
4
+ ```shell
5
+ simple_server.py
6
+ ```
7
+
8
+ 2. Configure the `llm_api_key` and `llm_base_url`, or relevant parameters.
9
+ 3. Run the pipeline:
10
+ ```shell
11
+ python run.py
12
+ ```
13
+
14
+ 4. View the logs in the console, and the following key information indicates successful execution:
15
+ ```text
16
+ mcp observation: container_id=None observer=None ability=None from_agent_name=None to_agent_name=None content='{"result": 25000.0}' dom_tree=None image=None action_result=[ActionResult(is_done=False, success=False, content='{"result": 25000.0}', error=None, keep=True, action_name='divide', tool_name='simple-calculator', tool_id='call_Sb4c16wDzUvdTPaqqqdNH6Er', metadata={})] images=[] info={}
17
+ ```
examples/mcp_demo/mcp_example.json ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "mcpServers": {
3
+ "amap-amap-sse": {
4
+ "url": "https://mcp.amap.com/sse?key=YOUR_API_KEY",
5
+ "timeout": 5.0,
6
+ "sse_read_timeout": 300.0
7
+ },
8
+ "tavily-mcp": {
9
+ "command": "npx",
10
+ "args": ["-y", "[email protected]"],
11
+ "env": {
12
+ "TAVILY_API_KEY": "YOUR_API_KEY"
13
+ }
14
+ }
15
+ }
16
+ }
examples/mcp_demo/prompt.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ system_prompt = """
2
+ <instruction>
3
+ You are an intelligent assistant.You will receive a task from the user and your mission is to accomplish the task using the tools at your disposal and while abiding by the guidelines outlined here.
4
+ </instruction>
5
+
6
+
7
+ <tool_list>
8
+ Now you can call the following tools to help users complete tasks.Each tool has different functions and parameters, defined as follows:
9
+ {tool_list}
10
+ ...(More tools can be added here, uniform format)
11
+ </tool_list>
12
+
13
+ <how to work>
14
+ 1.Selecting tools must be based on the tool descriptions combined with the user's query to choose the most suitable tool.
15
+ 2. If the user's question can be solved by calling the above tools, determine which tools to use and extract the parameter contents from the user's input.
16
+ 3. Your response **must strictly output in JSON structure**, no natural language description allowed, no Markdown formatting, no ```json or ``` tags, and no newline characters like ("\n", "\r", etc.), output the JSON string directly:
17
+ {{
18
+ "use_tool_list":[{{
19
+ "tool":"tool_name",
20
+ "arguments": {{
21
+ "param1_name": "param1_value",
22
+ "param2_name": "param2_value"
23
+ }}
24
+ }}]
25
+ }}
26
+ 4. If the user's question cannot be solved by the above tools, do not return an empty tool list, output only the final response.
27
+ 5. Important: Only return a pure JSON string without any extra formatting or markers.
28
+ 6.You can call the tools. Each time, select one tool to invoke; the result of invoking the tool will also be fed back to the large model. Based on the tool's result and the user’s question, the large model decides whether to continue choosing tools or directly output the final result. Recursive or dead-loop calls to tools are not allowed. If multiple consecutive calls to tools still fail to meet the user's needs, you must generate a final response using all existing tool results obtained.
29
+ </how to work>
30
+
31
+ """
32
+
33
+ agent_prompt = """
34
+ 1.The tools was called:
35
+ <action_list>
36
+ {action_list}
37
+ </action_list>
38
+
39
+ 2.the tool returned the result:
40
+ <tool_result>
41
+ {result}
42
+ </tool_result>
43
+
44
+ Please summarize the result based on the user's question and the tool's feedback, then decide whether to continue selecting other tools to complete the task or directly output the final result.
45
+ """
examples/mcp_demo/run.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding: utf-8
2
+ # Copyright (c) 2025 inclusionAI.
3
+
4
+ from aworld.config.conf import AgentConfig
5
+ from aworld.agents.llm_agent import Agent
6
+ from aworld.runner import Runners
7
+ from examples.mcp_demo.servers_config import mcp_config
8
+
9
+ if __name__ == '__main__':
10
+ agent_config = AgentConfig(
11
+ llm_provider="openai",
12
+ llm_model_name="gpt-4o",
13
+ llm_api_key="YOUR_API_KEY",
14
+ llm_base_url="http://localhost:5080"
15
+ )
16
+
17
+ search_sys_prompt = "You can use simple-calculator tools to calculate numbers and answer questions"
18
+ search = Agent(
19
+ conf=agent_config,
20
+ name="search_agent",
21
+ system_prompt=search_sys_prompt,
22
+ # mcp_servers=["amap-amap-sse"], # MCP server name for agent to use
23
+ mcp_servers = ["simple-calculator"], # MCP server name for agent to use
24
+ mcp_config=mcp_config
25
+ )
26
+
27
+ # Run agent
28
+ Runners.sync_run(input="30,000 divided by 1.2 ", agent=search)
examples/mcp_demo/servers_config.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ mcp_config = {
2
+ "mcpServers": {
3
+ "aworld": {
4
+ "url": "http://localhost:20000/sse"
5
+ },
6
+ "amap-amap-sse": {
7
+ "type": "sse",
8
+ "url": "https://mcp.amap.com/sse?key=YOUR_API_KEY",
9
+ "timeout": 5,
10
+ "sse_read_timeout": 300
11
+ },
12
+ "tavily-mcp": {
13
+ "type": "stdio",
14
+ "command": "npx",
15
+ "args": [
16
+ "-y",
17
18
+ ],
19
+ "env": {
20
+ "TAVILY_API_KEY": "YOUR_API_KEY"
21
+ }
22
+ },
23
+ "simple-calculator": {
24
+ "type": "sse",
25
+ "url": "http://127.0.0.1:8500/calculator/sse",
26
+ "timeout": 5,
27
+ "sse_read_timeout": 300
28
+ }
29
+ }
30
+ }
examples/mcp_demo/simple_server.py ADDED
@@ -0,0 +1,334 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding: utf-8
2
+ # Copyright (c) 2025 inclusionAI.
3
+
4
+ """
5
+ Simple calculator MCP server example.
6
+ """
7
+
8
+ import argparse
9
+ import os
10
+ import time
11
+ from typing import List, Dict, Any, Optional
12
+ from pydantic import Field
13
+
14
+ from aworld.mcp_client.decorator import mcp_server
15
+
16
+
17
+ @mcp_server(
18
+ name="simple-calculator",
19
+ mode="sse",
20
+ host="127.0.0.1",
21
+ port=8500,
22
+ sse_path="/calculator/sse",
23
+ auto_start=True # if False you can start manually in main()
24
+ )
25
+ class Calculator:
26
+ """Provides basic mathematical functions, including addition, subtraction, multiplication, division, and calculation history management."""
27
+
28
+ def __init__(self):
29
+ self.history = []
30
+
31
+ def add(self,
32
+ a: float = Field(description="First addend"),
33
+ b: float = Field(description="Second addend")
34
+ ) -> Dict[str, Any]:
35
+ """
36
+ Add two numbers
37
+
38
+ :param a: First addend
39
+ :param b: Second addend
40
+ :return: Dictionary containing the result
41
+ """
42
+ result = a + b
43
+ self.history.append(f"{a} + {b} = {result}")
44
+ print(f"add:{a} + {b} = {result}")
45
+ return {"result": result}
46
+
47
+ def subtract(self,
48
+ a: float = Field(description="Minuend"),
49
+ b: float = Field(description="Subtrahend")
50
+ ) -> Dict[str, Any]:
51
+ """
52
+ Subtract the second number from the first number
53
+
54
+ :param a: Minuend
55
+ :param b: Subtrahend
56
+ :return: Dictionary containing the result
57
+ """
58
+ result = a - b
59
+ self.history.append(f"{a} - {b} = {result}")
60
+ print(f"subtract:{a} - {b} = {result}")
61
+ return {"result": result}
62
+
63
+ def multiply(self,
64
+ a: float = Field(description="First factor"),
65
+ b: float = Field(description="Second factor")
66
+ ) -> Dict[str, Any]:
67
+ """
68
+ Multiply two numbers
69
+
70
+ :param a: First factor
71
+ :param b: Second factor
72
+ :return: Dictionary containing the result
73
+ """
74
+ result = a * b
75
+ self.history.append(f"{a} * {b} = {result}")
76
+ print(f"multiply:{a} * {b} = {result}")
77
+ return {"result": result}
78
+
79
+ def divide(self,
80
+ a: float = Field(description="Dividend"),
81
+ b: float = Field(description="Divisor")
82
+ ) -> Dict[str, Any]:
83
+ """
84
+ Divide the first number by the second number
85
+
86
+ :param a: Dividend
87
+ :param b: Divisor
88
+ :return: Dictionary containing the result
89
+ """
90
+ if b == 0:
91
+ raise ValueError("Divisor cannot be zero")
92
+ result = a / b
93
+ self.history.append(f"{a} / {b} = {result}")
94
+ print(f"divide:{a} / {b} = {result}")
95
+ return {"result": result}
96
+
97
+ def get_history(self) -> Dict[str, List[str]]:
98
+ """
99
+ Get calculation history
100
+
101
+ :return: Dictionary containing the history
102
+ """
103
+ return {"history": self.history}
104
+
105
+ def clear_history(self) -> Dict[str, str]:
106
+ """
107
+ Clear calculation history
108
+
109
+ :return: Dictionary containing operation status
110
+ """
111
+ self.history = []
112
+ return {"status": "History cleared"}
113
+
114
+
115
+ @mcp_server(
116
+ name="weather",
117
+ mode="sse",
118
+ host="127.0.0.1",
119
+ port=8200,
120
+ sse_path="/weather/sse",
121
+ auto_start=False # Don't auto-start, we'll start manually in main()
122
+ )
123
+ class WeatherService:
124
+ """A service that can query and manage city weather information, supports adding cities and getting city weather data."""
125
+
126
+ def __init__(self):
127
+ self.locations = {
128
+ "Beijing": {"temp": 20, "humidity": 60, "weather": "Sunny"},
129
+ "Shanghai": {"temp": 25, "humidity": 70, "weather": "Cloudy"},
130
+ "Guangzhou": {"temp": 30, "humidity": 80, "weather": "Rainy"}
131
+ }
132
+
133
+ def get_current_weather(self,
134
+ location: str = Field(description="City name")
135
+ ) -> Dict[str, Any]:
136
+ """
137
+ Get current weather for a specified city
138
+
139
+ :param location: City name
140
+ :return: Dictionary containing weather information
141
+ """
142
+ if location not in self.locations:
143
+ return {"error": f"City {location} does not exist"}
144
+ return {"weather": self.locations[location]}
145
+
146
+ def get_locations(self) -> Dict[str, List[str]]:
147
+ """
148
+ Get list of all available cities
149
+
150
+ :return: Dictionary containing city list
151
+ """
152
+ return {"locations": list(self.locations.keys())}
153
+
154
+ def add_location(self,
155
+ location: str = Field(description="City name"),
156
+ temp: float = Field(description="Temperature (Celsius)"),
157
+ humidity: float = Field(description="Humidity (percentage)"),
158
+ weather: str = Field(description="Weather description")
159
+ ) -> Dict[str, str]:
160
+ """
161
+ Add or update weather information for a city
162
+
163
+ :param location: City name
164
+ :param temp: Temperature (Celsius)
165
+ :param humidity: Humidity (percentage)
166
+ :param weather: Weather description
167
+ :return: Dictionary containing operation status
168
+ """
169
+ self.locations[location] = {
170
+ "temp": temp,
171
+ "humidity": humidity,
172
+ "weather": weather
173
+ }
174
+ return {"status": f"Weather information for {location} has been updated"}
175
+
176
+
177
+ @mcp_server(
178
+ name="async-calculator",
179
+ mode="sse",
180
+ host="127.0.0.1",
181
+ port=8200,
182
+ sse_path="/async-calculator/sse",
183
+ auto_start=False # Don't auto-start, we'll start manually in main()
184
+ )
185
+ class AsyncCalculator:
186
+ """Provides asynchronous version of basic mathematical functions, including addition, subtraction, multiplication, division, and calculation history management."""
187
+
188
+ def __init__(self):
189
+ self.history = []
190
+
191
+ async def add(self,
192
+ a: float = Field(description="First addend"),
193
+ b: float = Field(description="Second addend")
194
+ ) -> Dict[str, Any]:
195
+ """
196
+ Add two numbers (async version)
197
+
198
+ :param a: First addend
199
+ :param b: Second addend
200
+ :return: Dictionary containing the result
201
+ """
202
+ result = a + b
203
+ self.history.append(f"{a} + {b} = {result}")
204
+ print(f"async_add:{a} + {b} = {result}")
205
+ return {"result": result}
206
+
207
+ async def subtract(self,
208
+ a: float = Field(description="Minuend"),
209
+ b: float = Field(description="Subtrahend")
210
+ ) -> Dict[str, Any]:
211
+ """
212
+ Subtract the second number from the first number (async version)
213
+
214
+ :param a: Minuend
215
+ :param b: Subtrahend
216
+ :return: Dictionary containing the result
217
+ """
218
+ result = a - b
219
+ self.history.append(f"{a} - {b} = {result}")
220
+ print(f"async_subtract:{a} - {b} = {result}")
221
+ return {"result": result}
222
+
223
+ async def multiply(self,
224
+ a: float = Field(description="First factor"),
225
+ b: float = Field(description="Second factor")
226
+ ) -> Dict[str, Any]:
227
+ """
228
+ Multiply two numbers (async version)
229
+
230
+ :param a: First factor
231
+ :param b: Second factor
232
+ :return: Dictionary containing the result
233
+ """
234
+ result = a * b
235
+ self.history.append(f"{a} * {b} = {result}")
236
+ print(f"async_multiply:{a} * {b} = {result}")
237
+ return {"result": result}
238
+
239
+ async def divide(self,
240
+ a: float = Field(description="Dividend"),
241
+ b: float = Field(description="Divisor")
242
+ ) -> Dict[str, Any]:
243
+ """
244
+ Divide the first number by the second number (async version)
245
+
246
+ :param a: Dividend
247
+ :param b: Divisor
248
+ :return: Dictionary containing the result
249
+ """
250
+ if b == 0:
251
+ raise ValueError("Divisor cannot be zero")
252
+ result = a / b
253
+ self.history.append(f"{a} / {b} = {result}")
254
+ print(f"async_divide:{a} / {b} = {result}")
255
+ return {"result": result}
256
+
257
+ async def get_history(self) -> Dict[str, List[str]]:
258
+ """
259
+ Get calculation history (async version)
260
+
261
+ :return: Dictionary containing the history
262
+ """
263
+ return {"history": self.history}
264
+
265
+ async def clear_history(self) -> Dict[str, str]:
266
+ """
267
+ Clear calculation history (async version)
268
+
269
+ :return: Dictionary containing operation status
270
+ """
271
+ self.history = []
272
+ return {"status": "History cleared"}
273
+
274
+
275
+ def main():
276
+ parser = argparse.ArgumentParser(description="MCP Simple Calculator Server")
277
+ parser.add_argument("--server-type", choices=["calculator", "weather", "async-calculator"], default="calculator",
278
+ help="Server type, options: 'calculator', 'weather', or 'async-calculator'")
279
+ parser.add_argument("--mode", choices=["stdio", "sse"], default="sse",
280
+ help="Server running mode, options: 'stdio' or 'sse'")
281
+ parser.add_argument("--host", default="127.0.0.1", help="Server host address, default is 127.0.0.1")
282
+ parser.add_argument("--port", type=int, default=8200, help="Server port number, default is 8200")
283
+ parser.add_argument("--sse-path", default=None, help="SSE path, defaults based on server type")
284
+
285
+ args = parser.parse_args()
286
+
287
+ # Read configuration from environment variables if set
288
+ server_type = os.environ.get("MCP_SERVER_TYPE", args.server_type)
289
+ mode = os.environ.get("MCP_MODE", args.mode)
290
+ host = os.environ.get("MCP_HOST", args.host)
291
+
292
+ # Handle integer type
293
+ try:
294
+ port = int(os.environ.get("MCP_PORT", args.port))
295
+ except (ValueError, TypeError):
296
+ port = args.port
297
+
298
+ # Create server instance based on type
299
+ if server_type == "calculator":
300
+ server = Calculator()
301
+ default_sse_path = "/calculator/sse"
302
+ elif server_type == "async-calculator":
303
+ server = AsyncCalculator()
304
+ default_sse_path = "/async-calculator/sse"
305
+ else:
306
+ server = WeatherService()
307
+ default_sse_path = "/weather/sse"
308
+
309
+ # Set SSE path from args, env, or default
310
+ sse_path = os.environ.get("MCP_SSE_PATH", args.sse_path or default_sse_path)
311
+
312
+ print(f"Using configuration: server_type={server_type}, mode={mode}, host={host}, port={port}, sse_path={sse_path}")
313
+
314
+ # Run server with provided configuration
315
+ server.run(mode=mode, host=host, port=port, sse_path=sse_path)
316
+
317
+
318
+ def auto_start_example():
319
+
320
+ Calculator()
321
+
322
+ print("Auto-starting calculator has been initialized.")
323
+ print("Server is running in background. Press Ctrl+C to exit.")
324
+
325
+ try:
326
+ # Keep main thread alive
327
+ while True:
328
+ time.sleep(1)
329
+ except KeyboardInterrupt:
330
+ print("Exiting...")
331
+
332
+
333
+ if __name__ == "__main__":
334
+ auto_start_example()