furas

Python - MCP - Client with dynamic resource (Stackoverflow)

Sep 5th, 2025 (edited)
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. #!/usr/bin/env python3.13
  2.  
  3. # date: 2025.09.05
  4.  
  5. # [python - Why doesn't MCP server expose my resources? - Stack Overflow](https://stackoverflow.com/questions/79756235/why-doesnt-mcp-server-expose-my-resources?noredirect=1#comment140716418_79756235)
  6.  
  7. # Doc: [FastMC - Call Your Server](https://gofastmcp.com/getting-started/quickstart#call-your-server)
  8.  
  9. # Run:
  10. #     python client.py       # run with port 8000
  11. #     python client.py 7000  # run with port 7000
  12.  
  13. # Server needs:
  14. # - FastMCP:
  15. #     from fastmcp import FastMCP
  16. #     mcp.run(transport='http', port=7000)
  17. # - mcp.server.FastMCP:
  18. #     from mcp.server.fastmcp import FastMCP
  19. #     mcp = FastMCP("Demo", port=7000)
  20. #     mcp.run(transport='streamable-http')  # it doesn't allow to set `port` here - port has to be in FastMCP
  21.  
  22. import sys
  23. import asyncio
  24. from fastmcp import Client
  25.  
  26. if len(sys.argv) > 1:
  27.     PORT = int(sys.argv[1])
  28. else:
  29.     PORT = 8000   # usually I use this port with different server
  30.  
  31. client = Client(f"http://localhost:{PORT}/mcp")  # doc forgot `/mcp`
  32.  
  33. async def main():
  34.  
  35.     async with client:
  36.  
  37.         # it doesn't show "dynamic resources" (or rather "resource patterns")
  38.         resources = await client.list_resources()
  39.         print(f'--- resources ({len(resources)}) ---')
  40.         for item in resources:
  41.             print(item)
  42.  
  43.         tools = await client.list_tools()
  44.         print(f'--- tools ({len(tools)}) ---')
  45.         for item in tools:
  46.             print(item)
  47.  
  48.         prompts = await client.list_prompts()
  49.         print(f'--- prompts ({len(prompts)}) ---')
  50.         for item in prompts:
  51.             print(item)
  52.  
  53.         # it can use "dynamic resources" (or rather "resource patterns")
  54.         print('--- reading resources ---')
  55.         result = await client.read_resource('greeting://Adam')  # @mcp.resource('greeting://{name}')
  56.         #results = await client.read_resource('greeting:Adam')  # @mcp.resource('greeting:{name}')
  57.         print(results)
  58.         for item in results:
  59.             print(item.text)
  60.  
  61.         #print('--- call tools ---')
  62.         #name = "Adam"
  63.         #result = await client.call_tool("greet", {"name": name})
  64.         #print(result)
  65.         #for item in result.content:
  66.         #    print(item.text)
  67.  
  68. asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment