-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy path2_2_mcp_http_action_deepseek.py
More file actions
83 lines (71 loc) · 2.73 KB
/
Copy path2_2_mcp_http_action_deepseek.py
File metadata and controls
83 lines (71 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import socket
import subprocess
import sys
import time
from contextlib import suppress
from pathlib import Path
from _shared import create_deepseek_agent, print_action_results, print_response
def find_open_port() -> int:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.bind(("127.0.0.1", 0))
return int(sock.getsockname()[1])
def wait_for_port(host: str, port: int, timeout_seconds: float = 10.0):
deadline = time.time() + timeout_seconds
while time.time() < deadline:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.settimeout(0.5)
try:
sock.connect((host, port))
return
except OSError:
time.sleep(0.2)
raise RuntimeError(f"Timed out waiting for {host}:{port}")
agent = create_deepseek_agent(
"Use HTTP MCP actions for exact calculations. Prefer action execution over estimating."
)
if __name__ == "__main__":
server_script = Path(__file__).with_name("_calculator_mcp_server.py")
port = find_open_port()
env = os.environ.copy()
env["MCP_TRANSPORT"] = "http"
env["MCP_PORT"] = str(port)
process = subprocess.Popen([sys.executable, str(server_script)], env=env)
try:
wait_for_port("127.0.0.1", port)
agent.use_mcp(f"http://127.0.0.1:{port}/mcp")
turn = agent.input(
"Use the MCP actions to compute (100.25 + 55.5) * 1.08 and return the exact result."
)
records = agent.get_action_result(prompt=turn.prompt)
print_action_results(records)
result = turn.get_result()
print_response(result)
finally:
with suppress(Exception):
process.kill()
process.wait(timeout=5)
# Expected key output after configuring DeepSeek:
# The local MCP HTTP server starts on 127.0.0.1.
# [ACTION_RECORDS] includes MCP calculator action calls.
# The final computed value for (100.25 + 55.5) * 1.08 is 168.21.
# How it works:
# find_open_port() picks a free TCP port; _calculator_mcp_server.py is launched as a
# subprocess with MCP_TRANSPORT=http and MCP_PORT=<port>. wait_for_port() blocks until
# the server accepts connections, then agent.use_mcp("http://127.0.0.1:<port>/mcp")
# registers its tools over HTTP instead of stdio. The subprocess is killed in a
# finally block after the request completes.
#
# Flow:
# find_open_port() -> e.g. 51423
# subprocess: _calculator_mcp_server.py (HTTP mode on port 51423)
# | wait_for_port()
# v
# agent.use_mcp("http://127.0.0.1:51423/mcp")
# |
# v
# model plans: add(100.25, 55.5) -> 155.75, then multiply(155.75, 1.08) -> 168.21
# |
# v
# ActionResult records injected -> model reply: "The result is 168.21."
# finally: process.kill()