Repository navigation
Expand file tree
/
Copy pathclient.py
More file actions
236 lines (198 loc) · 9.32 KB
/
Copy pathclient.py
File metadata and controls
236 lines (198 loc) · 9.32 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import asyncio
import json
import os
import signal
import sys
from contextlib import AsyncExitStack
from anthropic import Anthropic
from dotenv import load_dotenv
from mcp import Client, MCPError, StdioServerParameters
from mcp_types import CallToolResult, EmbeddedResource, ImageContent, Implementation, TextContent, TextResourceContents
load_dotenv() # load environment variables from .env
ANTHROPIC_MODEL = "claude-sonnet-5"
# Sonnet 5 thinks adaptively unless told otherwise, and max_tokens caps thinking
# plus the reply, so leave room for both.
MAX_TOKENS = 10000
MAX_TOOL_TURNS = 10
SYSTEM_PROMPT = "You are a helpful assistant. Use the available tools when they help answer the question."
# Image types the Messages API accepts.
SUPPORTED_IMAGE_TYPES = {"image/jpeg", "image/png", "image/gif", "image/webp"}
def tool_result_content(result: CallToolResult) -> list[dict]:
"""Translate an MCP tool result into content for an Anthropic tool_result.
MCP and Anthropic content blocks differ, so each kind is converted. A kind
the model cannot take becomes a placeholder rather than vanishing, so the
model knows something was there.
"""
content: list[dict] = []
for block in result.content:
if isinstance(block, TextContent):
content.append({"type": "text", "text": block.text})
elif isinstance(block, ImageContent) and block.mime_type in SUPPORTED_IMAGE_TYPES:
content.append(
{
"type": "image",
"source": {"type": "base64", "media_type": block.mime_type, "data": block.data},
}
)
elif isinstance(block, EmbeddedResource) and isinstance(block.resource, TextResourceContents):
content.append({"type": "text", "text": block.resource.text})
else:
content.append({"type": "text", "text": f"[unsupported result type: {block.type}]"})
content = [block for block in content if block.get("text") != ""]
has_text = any(isinstance(block, TextContent) and block.text for block in result.content)
if result.structured_content is not None and not has_text:
content.append({"type": "text", "text": json.dumps(result.structured_content)})
return content
class MCPClient:
def __init__(self):
# Initialize session and client objects
self.client: Client | None = None
self.exit_stack = AsyncExitStack()
self._anthropic: Anthropic | None = None
self.tools: list[dict] = []
self.messages: list[dict] = []
@property
def anthropic(self) -> Anthropic:
"""Lazy-initialize Anthropic client when needed"""
if self._anthropic is None:
self._anthropic = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
return self._anthropic
async def connect_to_server(self, server_command: list[str]):
"""Connect to an MCP server
Args:
server_command: The command that starts the server, and its arguments
"""
server_params = StdioServerParameters(command=server_command[0], args=server_command[1:])
# Client launches the command itself when given StdioServerParameters.
# "auto", the SDK's default, probes server/discover, falling back to the
# 2025-11-25 handshake.
self.client = await self.exit_stack.enter_async_context(
Client(
server_params,
mode="auto",
client_info=Implementation(name="mcp-client-python", version="1.0.0"),
)
)
# The tools are listed once, here; this client doesn't follow later changes.
response = await self.client.list_tools()
self.tools = [
{"name": tool.name, "description": tool.description, "input_schema": tool.input_schema}
for tool in response.tools
]
print(
f"\nConnected over protocol {self.client.protocol_version} with tools:",
[tool.name for tool in response.tools],
)
async def process_query(self, query: str) -> str:
"""Process a query using Claude and available tools"""
history_length = len(self.messages)
self.messages.append({"role": "user", "content": query})
final_text = []
for turn in range(MAX_TOOL_TURNS + 1):
response = self.anthropic.messages.create(
model=ANTHROPIC_MODEL,
max_tokens=MAX_TOKENS,
system=SYSTEM_PROMPT,
messages=self.messages,
tools=self.tools,
)
if response.stop_reason == "refusal":
# Drop the refused query so a follow-up doesn't send it again.
del self.messages[history_length:]
final_text.append("[The model declined to answer; the query is not kept in history]")
break
final_text.extend(content.text for content in response.content if content.type == "text")
if response.stop_reason == "max_tokens":
final_text.append("[Reply cut off at the max_tokens limit]")
break
if response.stop_reason != "tool_use":
# The API rejects an empty assistant message, so an empty reply isn't kept.
if response.content:
self.messages.append({"role": "assistant", "content": response.content})
break
if turn == MAX_TOOL_TURNS:
final_text.append(f"[Stopped after {MAX_TOOL_TURNS} tool-use turns]")
break
tool_results = []
for tool_use in (content for content in response.content if content.type == "tool_use"):
final_text.append(f"[Calling tool {tool_use.name} with args {tool_use.input}]")
try:
# call_tool validates the result against the declared schema.
result = await self.client.call_tool(tool_use.name, tool_use.input)
except MCPError as error:
# A rejected call is the model's mistake to correct, so it goes
# back as a failed tool_result rather than ending the query.
# Every tool_use still needs a matching result.
final_text.append(f"[{tool_use.name} failed: {error}]")
tool_results.append(
{
"type": "tool_result",
"tool_use_id": tool_use.id,
"content": f"Tool call {tool_use.name} failed: {error}",
"is_error": True,
}
)
continue
# structured_content is data the application can use directly.
if isinstance(result.structured_content, list):
final_text.append(f"[{tool_use.name} returned {len(result.structured_content)} items]")
tool_results.append(
{
"type": "tool_result",
"tool_use_id": tool_use.id,
"content": tool_result_content(result),
"is_error": bool(result.is_error),
}
)
self.messages.append({"role": "assistant", "content": response.content})
self.messages.append({"role": "user", "content": tool_results})
return "\n".join(final_text)
async def chat_loop(self):
"""Run an interactive chat loop"""
print("\nMCP Client Started!")
print("Type your queries, 'clear' to forget the conversation, or 'quit' to exit.")
while True:
# input() blocks, so keep it off the event loop.
try:
query = (await asyncio.to_thread(input, "\nQuery: ")).strip()
except EOFError:
break
if query.lower() == "quit":
break
if not query:
continue
if query.lower() == "clear":
self.messages.clear()
print("\nConversation cleared.")
continue
history_length = len(self.messages)
try:
response = await self.process_query(query)
print("\n" + response)
except Exception as e:
# Drop the failed query so a follow-up doesn't send it again.
del self.messages[history_length:]
print(f"\nError: {str(e)}")
async def cleanup(self):
"""Clean up resources"""
await self.exit_stack.aclose()
async def main():
if len(sys.argv) < 2:
print("Usage: uv run client.py <server_command> [args...]", file=sys.stderr)
sys.exit(1)
client = MCPClient()
try:
await client.connect_to_server(sys.argv[1:])
# Check if we have a valid API key to continue
api_key = os.getenv("ANTHROPIC_API_KEY")
if not api_key:
print("\nNo ANTHROPIC_API_KEY found. To query these tools with Claude, set your API key:")
print(" export ANTHROPIC_API_KEY=your-api-key-here")
return
await client.chat_loop()
finally:
await client.cleanup()
if __name__ == "__main__":
# Ctrl-C exits at once; otherwise Python waits for the thread blocked in input().
signal.signal(signal.SIGINT, signal.SIG_DFL)
asyncio.run(main())