Connect to an MCP server via stdio and list its tools with parameter info.
(server_path: Path | str)
| 282 | |
| 283 | |
| 284 | async def _list_tools(server_path: Path | str) -> list[dict]: |
| 285 | """Connect to an MCP server via stdio and list its tools with parameter info.""" |
| 286 | from mcp import ClientSession |
| 287 | from mcp.client.stdio import stdio_client |
| 288 | |
| 289 | params = _make_stdio_params(server_path) |
| 290 | async with stdio_client(params) as (read, write): |
| 291 | async with ClientSession(read, write) as session: |
| 292 | await session.initialize() |
| 293 | result = await session.list_tools() |
| 294 | tools = [] |
| 295 | for t in result.tools: |
| 296 | schema = t.inputSchema or {} |
| 297 | props = schema.get("properties", {}) |
| 298 | required = set(schema.get("required", [])) |
| 299 | parameters = [ |
| 300 | { |
| 301 | "name": k, |
| 302 | "type": v.get("type", "any"), |
| 303 | "required": k in required, |
| 304 | } |
| 305 | for k, v in props.items() |
| 306 | ] |
| 307 | tools.append( |
| 308 | { |
| 309 | "name": t.name, |
| 310 | "description": t.description or "", |
| 311 | "parameters": parameters, |
| 312 | } |
| 313 | ) |
| 314 | return tools |
| 315 | |
| 316 | |
| 317 | async def _call_tool(server_path: Path | str, tool_name: str, args: dict) -> str: |
no test coverage detected