|
| 1 | +# Inquire Model Context Protocol (MCP) servers. |
| 2 | +# |
| 3 | +# Usage: |
| 4 | +# |
| 5 | +# uvx 'cratedb-toolkit[mcp]' query mcp inquire --format=markdown | sponge mcp-cratedb-landscape.md |
| 6 | +# |
| 7 | +# ruff: noqa: T201 |
| 8 | +import dataclasses |
| 9 | +import io |
| 10 | +import logging |
| 11 | +import typing as t |
| 12 | +from contextlib import redirect_stdout |
| 13 | + |
| 14 | +from cratedb_toolkit.query.mcp.model import McpServer |
| 15 | + |
| 16 | +from .util import McpServerCapabilities, to_json, to_yaml |
| 17 | + |
| 18 | +logger = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +@dataclasses.dataclass |
| 22 | +class McpServerInquiry: |
| 23 | + """ |
| 24 | + Inquire capabilities of MCP server, and format as JSON, Markdown, or YAML. |
| 25 | + """ |
| 26 | + |
| 27 | + servers: t.List[McpServer] |
| 28 | + |
| 29 | + title = "Database MCP servers for PostgreSQL and CrateDB" |
| 30 | + text = ( |
| 31 | + "This page provides an overview about a fragment of the MCP server landscape,\n" |
| 32 | + "focusing on those that are talking to PostgreSQL and CrateDB databases.\n" |
| 33 | + "\n" |
| 34 | + "It enumerates the most popular adapters, and includes their detailed API capabilities.\n" |
| 35 | + ) |
| 36 | + |
| 37 | + notes = ( |
| 38 | + ":::{note}\n" |
| 39 | + "This page was generated automatically, please do not edit manually. To rebuild, use this command:\n" |
| 40 | + "```shell\n" |
| 41 | + "uvx 'cratedb-toolkit[mcp]' query mcp inquire --format=markdown | sponge doc/query/mcp/landscape.md\n" |
| 42 | + "```\n" |
| 43 | + ":::\n" |
| 44 | + ) |
| 45 | + |
| 46 | + seealso = ( |
| 47 | + ":::{seealso}\n" |
| 48 | + "Ready-to-run example programs about all the adapters are available at\n" |
| 49 | + "https://github.com/crate/cratedb-examples/tree/main/framework/mcp.\n" |
| 50 | + ":::\n" |
| 51 | + ) |
| 52 | + |
| 53 | + def __post_init__(self): |
| 54 | + if not self.servers: |
| 55 | + msg = "No servers selected" |
| 56 | + logger.error(msg) |
| 57 | + raise UserWarning(msg) |
| 58 | + |
| 59 | + @staticmethod |
| 60 | + async def get_capabilities(server: McpServer): |
| 61 | + """ |
| 62 | + Launch MCP server with stdio transport, and inquire API for capabilities. |
| 63 | +
|
| 64 | + Derived from: |
| 65 | + https://github.com/modelcontextprotocol/python-sdk?tab=readme-ov-file#writing-mcp-clients |
| 66 | + """ |
| 67 | + from mcp import ClientSession, StdioServerParameters |
| 68 | + from mcp.client.stdio import stdio_client |
| 69 | + |
| 70 | + if server.program is None: |
| 71 | + raise ValueError("Program name for MCP server not defined") |
| 72 | + |
| 73 | + # Create server parameters for stdio connection. |
| 74 | + server_params = StdioServerParameters( |
| 75 | + command=server.program, |
| 76 | + args=server.args, |
| 77 | + env=server.env, |
| 78 | + ) |
| 79 | + |
| 80 | + async with stdio_client(server_params) as (read, write): |
| 81 | + async with ClientSession(read, write) as session: |
| 82 | + # Initialize the connection. |
| 83 | + await session.initialize() |
| 84 | + |
| 85 | + # Inquire API. |
| 86 | + response = McpServerCapabilities(session) |
| 87 | + await response.inquire() |
| 88 | + return response |
| 89 | + |
| 90 | + async def format(self, variant: str): |
| 91 | + if variant == "json": |
| 92 | + return await self.to_json() |
| 93 | + elif variant == "markdown": |
| 94 | + return await self.to_markdown() |
| 95 | + elif variant == "yaml": |
| 96 | + return await self.to_yaml() |
| 97 | + else: |
| 98 | + raise NotImplementedError(f"Output variant not implemented: {variant}") |
| 99 | + |
| 100 | + async def to_dict(self): |
| 101 | + payload: t.Dict[str, t.Any] = { |
| 102 | + "meta": {"title": self.title, "text": self.text, "notes": self.notes, "seealso": self.seealso}, |
| 103 | + "data": {}, |
| 104 | + } |
| 105 | + for server in self.servers: |
| 106 | + capabilities = await self.get_capabilities(server) |
| 107 | + payload["data"][server.name] = { |
| 108 | + "meta": server.to_dict(), |
| 109 | + "capabilities": capabilities.to_dict(), |
| 110 | + } |
| 111 | + return payload |
| 112 | + |
| 113 | + async def to_markdown(self): |
| 114 | + buffer = io.StringIO() |
| 115 | + with redirect_stdout(buffer): |
| 116 | + print(f"# {self.title}") |
| 117 | + print() |
| 118 | + print(self.text) |
| 119 | + for server in self.servers: |
| 120 | + print(server.to_markdown()) |
| 121 | + try: |
| 122 | + capabilities = await self.get_capabilities(server) |
| 123 | + print(capabilities.to_markdown()) |
| 124 | + except Exception as ex: |
| 125 | + logger.error(f"MCP server capability inquiry failed: {ex}") |
| 126 | + print(self.notes) |
| 127 | + print(self.seealso) |
| 128 | + return buffer.getvalue() |
| 129 | + |
| 130 | + async def to_json(self): |
| 131 | + return to_json(await self.to_dict()) |
| 132 | + |
| 133 | + async def to_yaml(self): |
| 134 | + return to_yaml(await self.to_dict()) |
0 commit comments