forked from EricLBuehler/mistral.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_client.py
More file actions
60 lines (49 loc) · 1.75 KB
/
mcp_client.py
File metadata and controls
60 lines (49 loc) · 1.75 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
"""
MCP (Model Context Protocol) client usage with mistral.rs Python API.
Connects to an MCP server, auto-discovers tools, and makes them available
to the model during conversations.
Install the filesystem server first: npx @modelcontextprotocol/server-filesystem . -y
"""
import asyncio
import mistralrs
async def main():
# Connect to a local filesystem MCP server
mcp_config = mistralrs.McpClientConfigPy(
servers=[
mistralrs.McpServerConfigPy(
name="Filesystem Tools",
source=mistralrs.McpServerSourcePy.Process(
command="npx",
args=["@modelcontextprotocol/server-filesystem", "."],
work_dir=None,
env=None,
),
)
]
)
# Other transport types are also supported:
#
# McpServerSourcePy.Http(url="https://hf.co/mcp", timeout_secs=30, headers=None)
# McpServerSourcePy.WebSocket(url="wss://api.example.com/mcp", timeout_secs=30, headers=None)
#
# For authentication, set bearer_token="your-token".
# To avoid tool name conflicts, set tool_prefix="prefix".
runner = mistralrs.Runner(
which=mistralrs.Which.Plain(
model_id="Qwen/Qwen3-4B",
arch=mistralrs.Architecture.Qwen3,
),
mcp_client_config=mcp_config,
)
request = mistralrs.ChatCompletionRequest(
model="default",
messages=[
{"role": "user", "content": "List the files in the current directory."}
],
max_tokens=1000,
tool_choice="auto",
)
response = await runner.send_chat_completion_request(request)
print(response.choices[0].message.content)
if __name__ == "__main__":
asyncio.run(main())