-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
54 lines (46 loc) · 1.63 KB
/
test.py
File metadata and controls
54 lines (46 loc) · 1.63 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
import shutil
import os
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from mcp import types
server_params = StdioServerParameters(
command = str(shutil.which("npx")),
args = ["-y", "@modelcontextprotocol/server-everything"],
env=os.environ.copy()
)
async def handle_sampling_message(message: types.CreateMessageRequestParams) -> types.CreateMessageResult:
print(message)
return types.CreateMessageResult(
role="assistant",
content=types.TextContent(
type="text",
text="Hello, world! from model",
),
model="gpt-3.5-turbo",
stopReason="endTurn",
)
async def run():
print("Starting server")
async with stdio_client(server_params) as (read, write):
print("Connected")
async with ClientSession(read, write, sampling_callback=handle_sampling_message) as session:
print("Session created")
# Initialize the connection
await session.initialize()
print("Initialized")
print(f"{len((await session.list_tools()).tools)=}")
# Create a sampling request
resp = await session.call_tool(
name="sampleLLM",
arguments={
"prompt": "Hello, world!",
"max_tokens": 10,
}
)
assert resp.content[0].type == "text"
print(f"{resp.content[0].text=}")
print("Done")
exit(0)
if __name__ == "__main__":
import asyncio
asyncio.run(run())