feat: Add support for streamable http
Some checks failed
CI / test (push) Successful in 31s
CI / publish (push) Failing after 7s

This commit is contained in:
2026-03-31 10:47:23 +02:00
parent 0bdb817f26
commit 170348c3f2
2 changed files with 34 additions and 13 deletions

View File

@@ -5,14 +5,19 @@ from __future__ import annotations
import json
from dataclasses import dataclass, field
from pathlib import Path
from typing import Literal
@dataclass
class ServerConfig:
name: str
command: str
transport: Literal["stdio", "streamable_http"] = "stdio"
# stdio
command: str | None = None
args: list[str] = field(default_factory=list)
env: dict[str, str] | None = None
# streamable_http
url: str | None = None
def load_claude_json(path: Path | None = None) -> dict[str, ServerConfig]:
@@ -27,8 +32,17 @@ def from_dict(servers: dict) -> dict[str, ServerConfig]:
"""Parse a dict in the claude.json mcpServers format."""
result: dict[str, ServerConfig] = {}
for name, cfg in servers.items():
transport = cfg.get("transport", "stdio")
if transport == "streamable_http":
result[name] = ServerConfig(
name=name,
transport="streamable_http",
url=cfg["url"],
)
else:
result[name] = ServerConfig(
name=name,
transport="stdio",
command=cfg["command"],
args=cfg.get("args", []),
env=cfg.get("env"),

View File

@@ -7,6 +7,7 @@ from contextlib import AsyncExitStack
from mcp import ClientSession, StdioServerParameters
from mcp import types
from mcp.client.stdio import stdio_client
from mcp.client.streamable_http import streamable_http_client
from .config import ServerConfig
@@ -20,12 +21,18 @@ class ServerSession:
self._session: ClientSession | None = None
async def connect(self) -> None:
if self._config.transport == "streamable_http":
read, write, _ = await self._stack.enter_async_context(
streamable_http_client(self._config.url)
)
else:
params = StdioServerParameters(
command=self._config.command,
args=self._config.args,
env=self._config.env,
)
read, write = await self._stack.enter_async_context(stdio_client(params))
self._session = await self._stack.enter_async_context(
ClientSession(read, write)
)