Skip to content

Commit 8acb5f9

Browse files
committed
fix: fix client code
1 parent 8873262 commit 8acb5f9

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

quickstart/client.mdx

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Before starting, ensure your system meets these requirements:
1919

2020
## Setting Up Your Environment
2121

22-
First, create a new Python project with `uv`:
22+
First, create a new Python project with `uv`:
2323

2424
```bash
2525
# Create project directory
@@ -104,28 +104,28 @@ Next, we'll implement the method to connect to an MCP server:
104104
```python
105105
async def connect_to_server(self, server_script_path: str):
106106
"""Connect to an MCP server
107-
107+
108108
Args:
109109
server_script_path: Path to the server script (.py or .js)
110110
"""
111111
is_python = server_script_path.endswith('.py')
112112
is_js = server_script_path.endswith('.js')
113113
if not (is_python or is_js):
114114
raise ValueError("Server script must be a .py or .js file")
115-
115+
116116
command = "python" if is_python else "node"
117117
server_params = StdioServerParameters(
118118
command=command,
119119
args=[server_script_path],
120120
env=None
121121
)
122-
122+
123123
stdio_transport = await self.exit_stack.enter_async_context(stdio_client(server_params))
124124
self.stdio, self.write = stdio_transport
125125
self.session = await self.exit_stack.enter_async_context(ClientSession(self.stdio, self.write))
126-
126+
127127
await self.session.initialize()
128-
128+
129129
# List available tools
130130
response = await self.session.list_tools()
131131
tools = response.tools
@@ -147,7 +147,7 @@ async def process_query(self, query: str) -> str:
147147
]
148148

149149
response = await self.session.list_tools()
150-
available_tools = [{
150+
available_tools = [{
151151
"name": tool.name,
152152
"description": tool.description,
153153
"input_schema": tool.inputSchema
@@ -165,27 +165,34 @@ async def process_query(self, query: str) -> str:
165165
tool_results = []
166166
final_text = []
167167

168+
assistant_message_content = []
168169
for content in response.content:
169170
if content.type == 'text':
170171
final_text.append(content.text)
172+
assistant_message_content.append(content)
171173
elif content.type == 'tool_use':
172174
tool_name = content.name
173175
tool_args = content.input
174-
176+
175177
# Execute tool call
176178
result = await self.session.call_tool(tool_name, tool_args)
177179
tool_results.append({"call": tool_name, "result": result})
178180
final_text.append(f"[Calling tool {tool_name} with args {tool_args}]")
179181

180-
# Continue conversation with tool results
181-
if hasattr(content, 'text') and content.text:
182-
messages.append({
183-
"role": "assistant",
184-
"content": content.text
185-
})
182+
assistant_message_content.append(content)
186183
messages.append({
187-
"role": "user",
188-
"content": result.content
184+
"role": "assistant",
185+
"content": assistant_message_content
186+
})
187+
messages.append({
188+
"role": "user",
189+
"content": [
190+
{
191+
"type": "tool_result",
192+
"tool_use_id": content.id,
193+
"content": result.content
194+
}
195+
]
189196
})
190197

191198
# Get next response from Claude
@@ -208,17 +215,17 @@ async def chat_loop(self):
208215
"""Run an interactive chat loop"""
209216
print("\nMCP Client Started!")
210217
print("Type your queries or 'quit' to exit.")
211-
218+
212219
while True:
213220
try:
214221
query = input("\nQuery: ").strip()
215-
222+
216223
if query.lower() == 'quit':
217224
break
218-
225+
219226
response = await self.process_query(query)
220227
print("\n" + response)
221-
228+
222229
except Exception as e:
223230
print(f"\nError: {str(e)}")
224231

@@ -236,7 +243,7 @@ async def main():
236243
if len(sys.argv) < 2:
237244
print("Usage: python client.py <path_to_server_script>")
238245
sys.exit(1)
239-
246+
240247
client = MCPClient()
241248
try:
242249
await client.connect_to_server(sys.argv[1])
@@ -427,4 +434,4 @@ If you see:
427434
>
428435
Understand how MCP connects clients, servers, and LLMs
429436
</Card>
430-
</CardGroup>
437+
</CardGroup>

0 commit comments

Comments
 (0)