Merge branch 'main' into fix-ts-client-tool-use
This commit is contained in:
commit
bea3d53731
1 changed files with 41 additions and 31 deletions
|
|
@ -12,6 +12,7 @@ load_dotenv() # load environment variables from .env
|
||||||
|
|
||||||
# Claude model constant
|
# Claude model constant
|
||||||
ANTHROPIC_MODEL = "claude-sonnet-4-5"
|
ANTHROPIC_MODEL = "claude-sonnet-4-5"
|
||||||
|
MAX_TOOL_TURNS = 10
|
||||||
|
|
||||||
|
|
||||||
class MCPClient:
|
class MCPClient:
|
||||||
|
|
@ -64,45 +65,52 @@ class MCPClient:
|
||||||
"""Process a query using Claude and available tools"""
|
"""Process a query using Claude and available tools"""
|
||||||
messages = [{"role": "user", "content": query}]
|
messages = [{"role": "user", "content": query}]
|
||||||
|
|
||||||
response = await self.session.list_tools()
|
tools_response = await self.session.list_tools()
|
||||||
available_tools = [
|
available_tools = [
|
||||||
{"name": tool.name, "description": tool.description, "input_schema": tool.inputSchema}
|
{"name": tool.name, "description": tool.description, "input_schema": tool.inputSchema}
|
||||||
for tool in response.tools
|
for tool in tools_response.tools
|
||||||
]
|
]
|
||||||
|
|
||||||
# Initial Claude API call
|
final_text = []
|
||||||
|
|
||||||
response = self.anthropic.messages.create(
|
response = self.anthropic.messages.create(
|
||||||
model=ANTHROPIC_MODEL, max_tokens=1000, messages=messages, tools=available_tools
|
model=ANTHROPIC_MODEL, max_tokens=1000, messages=messages, tools=available_tools
|
||||||
)
|
)
|
||||||
|
|
||||||
# Process response and handle tool calls
|
for _ in range(MAX_TOOL_TURNS):
|
||||||
final_text = []
|
tool_uses = []
|
||||||
|
for content in response.content:
|
||||||
|
if content.type == "text":
|
||||||
|
final_text.append(content.text)
|
||||||
|
elif content.type == "tool_use":
|
||||||
|
tool_uses.append(content)
|
||||||
|
|
||||||
for content in response.content:
|
if not tool_uses:
|
||||||
if content.type == "text":
|
return "\n".join(final_text)
|
||||||
final_text.append(content.text)
|
|
||||||
elif content.type == "tool_use":
|
|
||||||
tool_name = content.name
|
|
||||||
tool_args = content.input
|
|
||||||
|
|
||||||
# Execute tool call
|
tool_results = []
|
||||||
result = await self.session.call_tool(tool_name, tool_args)
|
for tool_use in tool_uses:
|
||||||
final_text.append(f"[Calling tool {tool_name} with args {tool_args}]")
|
result = await self.session.call_tool(tool_use.name, tool_use.input)
|
||||||
|
final_text.append(f"[Calling tool {tool_use.name} with args {tool_use.input}]")
|
||||||
# Continue conversation with tool results
|
tool_results.append(
|
||||||
if hasattr(content, "text") and content.text:
|
{
|
||||||
messages.append({"role": "assistant", "content": content.text})
|
"type": "tool_result",
|
||||||
messages.append({"role": "user", "content": result.content})
|
"tool_use_id": tool_use.id,
|
||||||
|
"content": result.content,
|
||||||
# Get next response from Claude
|
}
|
||||||
response = self.anthropic.messages.create(
|
|
||||||
model=ANTHROPIC_MODEL,
|
|
||||||
max_tokens=1000,
|
|
||||||
messages=messages,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
final_text.append(response.content[0].text)
|
messages.append({"role": "assistant", "content": response.content})
|
||||||
|
messages.append({"role": "user", "content": tool_results})
|
||||||
|
|
||||||
|
response = self.anthropic.messages.create(
|
||||||
|
model=ANTHROPIC_MODEL,
|
||||||
|
max_tokens=1000,
|
||||||
|
messages=messages,
|
||||||
|
tools=available_tools,
|
||||||
|
)
|
||||||
|
|
||||||
|
final_text.append(f"[Stopped after {MAX_TOOL_TURNS} tool-use turns]")
|
||||||
return "\n".join(final_text)
|
return "\n".join(final_text)
|
||||||
|
|
||||||
async def chat_loop(self):
|
async def chat_loop(self):
|
||||||
|
|
@ -113,13 +121,15 @@ class MCPClient:
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
query = input("\nQuery: ").strip()
|
query = input("\nQuery: ").strip()
|
||||||
|
except (EOFError, KeyboardInterrupt):
|
||||||
|
break
|
||||||
|
|
||||||
if query.lower() == "quit":
|
if query.lower() == "quit":
|
||||||
break
|
break
|
||||||
|
|
||||||
|
try:
|
||||||
response = await self.process_query(query)
|
response = await self.process_query(query)
|
||||||
print("\n" + response)
|
print("\n" + response)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"\nError: {str(e)}")
|
print(f"\nError: {str(e)}")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue