Eddyhzd commited on
Commit
35b235c
·
1 Parent(s): 2fc1a4a
Files changed (1) hide show
  1. app.py +15 -69
app.py CHANGED
@@ -1,83 +1,29 @@
1
  """
2
- cd to the `examples/snippets/clients` directory and run:
3
- uv run client
4
  """
5
 
6
  import asyncio
7
- import os
8
 
9
- from pydantic import AnyUrl
 
10
 
11
- from mcp import ClientSession, StdioServerParameters, types
12
- from mcp.client.stdio import stdio_client
13
- from mcp.shared.context import RequestContext
14
 
15
- # Create server parameters for stdio connection
16
- server_params = StdioServerParameters(
17
- command="uv", # Using uv to run the server
18
- args=["run", "server", "fastmcp_quickstart", "stdio"], # We're already in snippets dir
19
- env={"UV_INDEX": os.environ.get("UV_INDEX", "")},
20
- )
21
-
22
-
23
- # Optional: create a sampling callback
24
- async def handle_sampling_message(
25
- context: RequestContext[ClientSession, None], params: types.CreateMessageRequestParams
26
- ) -> types.CreateMessageResult:
27
- print(f"Sampling request: {params.messages}")
28
- return types.CreateMessageResult(
29
- role="assistant",
30
- content=types.TextContent(
31
- type="text",
32
- text="Hello, world! from model",
33
- ),
34
- model="gpt-3.5-turbo",
35
- stopReason="endTurn",
36
- )
37
-
38
-
39
- async def run():
40
- async with stdio_client(server_params) as (read, write):
41
- async with ClientSession(read, write, sampling_callback=handle_sampling_message) as session:
42
  # Initialize the connection
43
  await session.initialize()
44
-
45
- # List available prompts
46
- prompts = await session.list_prompts()
47
- print(f"Available prompts: {[p.name for p in prompts.prompts]}")
48
-
49
- # Get a prompt (greet_user prompt from fastmcp_quickstart)
50
- if prompts.prompts:
51
- prompt = await session.get_prompt("greet_user", arguments={"name": "Alice", "style": "friendly"})
52
- print(f"Prompt result: {prompt.messages[0].content}")
53
-
54
- # List available resources
55
- resources = await session.list_resources()
56
- print(f"Available resources: {[r.uri for r in resources.resources]}")
57
-
58
  # List available tools
59
  tools = await session.list_tools()
60
- print(f"Available tools: {[t.name for t in tools.tools]}")
61
-
62
- # Read a resource (greeting resource from fastmcp_quickstart)
63
- resource_content = await session.read_resource(AnyUrl("greeting://World"))
64
- content_block = resource_content.contents[0]
65
- if isinstance(content_block, types.TextContent):
66
- print(f"Resource content: {content_block.text}")
67
-
68
- # Call a tool (add tool from fastmcp_quickstart)
69
- result = await session.call_tool("add", arguments={"a": 5, "b": 3})
70
- result_unstructured = result.content[0]
71
- if isinstance(result_unstructured, types.TextContent):
72
- print(f"Tool result: {result_unstructured.text}")
73
- result_structured = result.structuredContent
74
- print(f"Structured tool result: {result_structured}")
75
-
76
-
77
- def main():
78
- """Entry point for the client script."""
79
- asyncio.run(run())
80
 
81
 
82
  if __name__ == "__main__":
83
- main()
 
1
  """
2
+ Run from the repository root:
3
+ uv run examples/snippets/clients/streamable_basic.py
4
  """
5
 
6
  import asyncio
 
7
 
8
+ from mcp import ClientSession
9
+ from mcp.client.streamable_http import streamablehttp_client
10
 
 
 
 
11
 
12
+ async def main():
13
+ # Connect to a streamable HTTP server
14
+ async with streamablehttp_client("http://localhost:8000/mcp") as (
15
+ read_stream,
16
+ write_stream,
17
+ _,
18
+ ):
19
+ # Create a session using the client streams
20
+ async with ClientSession(read_stream, write_stream) as session:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  # Initialize the connection
22
  await session.initialize()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  # List available tools
24
  tools = await session.list_tools()
25
+ print(f"Available tools: {[tool.name for tool in tools.tools]}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
 
28
  if __name__ == "__main__":
29
+ asyncio.run(main())