feat: Add support for streamable http
This commit is contained in:
@@ -5,14 +5,19 @@ from __future__ import annotations
|
|||||||
import json
|
import json
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import Literal
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class ServerConfig:
|
class ServerConfig:
|
||||||
name: str
|
name: str
|
||||||
command: str
|
transport: Literal["stdio", "streamable_http"] = "stdio"
|
||||||
|
# stdio
|
||||||
|
command: str | None = None
|
||||||
args: list[str] = field(default_factory=list)
|
args: list[str] = field(default_factory=list)
|
||||||
env: dict[str, str] | None = None
|
env: dict[str, str] | None = None
|
||||||
|
# streamable_http
|
||||||
|
url: str | None = None
|
||||||
|
|
||||||
|
|
||||||
def load_claude_json(path: Path | None = None) -> dict[str, ServerConfig]:
|
def load_claude_json(path: Path | None = None) -> dict[str, ServerConfig]:
|
||||||
@@ -27,10 +32,19 @@ def from_dict(servers: dict) -> dict[str, ServerConfig]:
|
|||||||
"""Parse a dict in the claude.json mcpServers format."""
|
"""Parse a dict in the claude.json mcpServers format."""
|
||||||
result: dict[str, ServerConfig] = {}
|
result: dict[str, ServerConfig] = {}
|
||||||
for name, cfg in servers.items():
|
for name, cfg in servers.items():
|
||||||
result[name] = ServerConfig(
|
transport = cfg.get("transport", "stdio")
|
||||||
name=name,
|
if transport == "streamable_http":
|
||||||
command=cfg["command"],
|
result[name] = ServerConfig(
|
||||||
args=cfg.get("args", []),
|
name=name,
|
||||||
env=cfg.get("env"),
|
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"),
|
||||||
|
)
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ from contextlib import AsyncExitStack
|
|||||||
from mcp import ClientSession, StdioServerParameters
|
from mcp import ClientSession, StdioServerParameters
|
||||||
from mcp import types
|
from mcp import types
|
||||||
from mcp.client.stdio import stdio_client
|
from mcp.client.stdio import stdio_client
|
||||||
|
from mcp.client.streamable_http import streamable_http_client
|
||||||
|
|
||||||
from .config import ServerConfig
|
from .config import ServerConfig
|
||||||
|
|
||||||
@@ -20,12 +21,18 @@ class ServerSession:
|
|||||||
self._session: ClientSession | None = None
|
self._session: ClientSession | None = None
|
||||||
|
|
||||||
async def connect(self) -> None:
|
async def connect(self) -> None:
|
||||||
params = StdioServerParameters(
|
if self._config.transport == "streamable_http":
|
||||||
command=self._config.command,
|
read, write, _ = await self._stack.enter_async_context(
|
||||||
args=self._config.args,
|
streamable_http_client(self._config.url)
|
||||||
env=self._config.env,
|
)
|
||||||
)
|
else:
|
||||||
read, write = await self._stack.enter_async_context(stdio_client(params))
|
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(
|
self._session = await self._stack.enter_async_context(
|
||||||
ClientSession(read, write)
|
ClientSession(read, write)
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user