Guest User

Untitled

a guest
Aug 18th, 2025
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. import asyncio
  2. import warnings
  3. from fastmcp import Client
  4.  
  5. # Suppress specific deprecation warnings
  6. warnings.filterwarnings("ignore", category=DeprecationWarning, message=".*httpx.*")
  7. warnings.filterwarnings("ignore", category=DeprecationWarning, message=".*pydantic.*")
  8.  
  9. # Standard MCP configuration with multiple servers
  10. config = {
  11. "mcpServers": {
  12. "context7": {"url": "https://mcp.context7.com/mcp"},
  13. }
  14. }
  15.  
  16. # Create a client that connects to all servers
  17. client = Client(config)
  18.  
  19. async def resolve_library_id_async(library_name: str):
  20. """
  21. Resolve a library name to a unique library ID using the Context7 API.
  22. This is an async function.
  23. """
  24. async with client:
  25. response = await client.call_tool("resolve-library-id", {"libraryName": library_name})
  26. return response
  27.  
  28. async def get_library_docs_async(library_id: str, topic: str = "", tokens: int = 10000):
  29. """
  30. Get the documentation for a library using its ID and optional topic and token count.
  31. This is an async function.
  32. """
  33. params = {
  34. "context7CompatibleLibraryID": str(library_id), # Ensure library_id is a string
  35. "topic": topic, # Pass an empty string for topic if None
  36. "tokens": tokens
  37. }
  38. async with client:
  39. response = await client.call_tool("get-library-docs", params)
  40. return response
  41.  
  42. def resolve_library_id(library_name: str):
  43. """
  44. Synchronous wrapper for resolving library ID.
  45. Runs inside the current event loop if one exists.
  46. """
  47. # Check if we are already in an event loop
  48. if asyncio.get_event_loop().is_running():
  49. return asyncio.ensure_future(resolve_library_id_async(library_name))
  50. else:
  51. return asyncio.run(resolve_library_id_async(library_name))
  52.  
  53. def get_library_docs(library_id: str, topic: str = "", tokens: int = 10000):
  54. """
  55. Synchronous wrapper for getting library documentation.
  56. Runs inside the current event loop if one exists.
  57. """
  58. # Check if we are already in an event loop
  59. if asyncio.get_event_loop().is_running():
  60. return asyncio.ensure_future(get_library_docs_async(library_id, topic, tokens))
  61. else:
  62. return asyncio.run(get_library_docs_async(library_id, topic, tokens))
  63.  
Advertisement
Add Comment
Please, Sign In to add comment