fix: preserve mcp source compatibility
This commit is contained in:
@@ -6,12 +6,15 @@ from dataclasses import asdict, dataclass, field
|
||||
from inspect import isawaitable
|
||||
from typing import Any, cast
|
||||
|
||||
from wf_sources_mcp.connections import McpSourceConnection
|
||||
from wf_sources_mcp.sdk import ToolCallResult
|
||||
from wf_sources_mcp.transports import HttpSourceTransport, StdioSourceTransport
|
||||
|
||||
from ..auth import AuthRecord
|
||||
from ..models import ConnectionConfig
|
||||
from .session import PersistentMcpSession
|
||||
|
||||
RuntimeConnection = ConnectionConfig | McpSourceConnection
|
||||
SessionFactory = Callable[
|
||||
[ConnectionConfig, AuthRecord | None],
|
||||
PersistentMcpSession | Awaitable[PersistentMcpSession],
|
||||
@@ -19,7 +22,7 @@ SessionFactory = Callable[
|
||||
|
||||
|
||||
def connection_runtime_fingerprint(
|
||||
connection: ConnectionConfig,
|
||||
connection: RuntimeConnection,
|
||||
auth: AuthRecord | None = None,
|
||||
) -> str:
|
||||
"""Return the connection identity that decides MCP runtime reuse.
|
||||
@@ -55,7 +58,7 @@ class McpRuntimePool:
|
||||
|
||||
async def get_session(
|
||||
self,
|
||||
connection: ConnectionConfig,
|
||||
connection: RuntimeConnection,
|
||||
auth: AuthRecord | None,
|
||||
) -> PersistentMcpSession:
|
||||
fingerprint = connection_runtime_fingerprint(connection, auth)
|
||||
@@ -65,7 +68,10 @@ class McpRuntimePool:
|
||||
if current is not None:
|
||||
await current[1].close()
|
||||
|
||||
created = self.session_factory(connection, auth)
|
||||
# Compatibility boundary: wrappers now pass McpSourceConnection, while
|
||||
# PersistentSessionFactory still consumes the legacy broker DTO until
|
||||
# the shared opener/runtime move lands.
|
||||
created = self.session_factory(_legacy_connection_config(connection), auth)
|
||||
if isawaitable(created):
|
||||
session = await created
|
||||
else:
|
||||
@@ -75,8 +81,8 @@ class McpRuntimePool:
|
||||
|
||||
async def call_tool(
|
||||
self,
|
||||
connection,
|
||||
auth,
|
||||
connection: RuntimeConnection,
|
||||
auth: AuthRecord | None,
|
||||
tool_name: str,
|
||||
payload: dict[str, Any],
|
||||
) -> ToolCallResult:
|
||||
@@ -94,3 +100,45 @@ class McpRuntimePool:
|
||||
self._sessions.clear()
|
||||
for _fingerprint, session in sessions:
|
||||
await session.close()
|
||||
|
||||
|
||||
def _legacy_connection_config(connection: RuntimeConnection) -> ConnectionConfig:
|
||||
if isinstance(connection, ConnectionConfig):
|
||||
return connection
|
||||
|
||||
metadata = dict(connection.metadata)
|
||||
transport = connection.transport
|
||||
if isinstance(transport, StdioSourceTransport):
|
||||
metadata.update(
|
||||
{
|
||||
"transport": "stdio",
|
||||
"command": transport.command,
|
||||
"args": list(transport.args),
|
||||
"env": dict(transport.env),
|
||||
}
|
||||
)
|
||||
if transport.cwd is not None:
|
||||
metadata["cwd"] = transport.cwd
|
||||
elif isinstance(transport, HttpSourceTransport):
|
||||
metadata.update(
|
||||
{
|
||||
"transport": "streamable_http",
|
||||
"url": str(transport.url),
|
||||
"headers": dict(transport.headers),
|
||||
}
|
||||
)
|
||||
else:
|
||||
raise ValueError(f"connection {connection.id!r} requires metadata.transport")
|
||||
|
||||
if connection.profile is not None:
|
||||
metadata["profile"] = connection.profile
|
||||
if connection.auth_ref is not None:
|
||||
metadata["auth_ref"] = connection.auth_ref
|
||||
|
||||
return ConnectionConfig(
|
||||
id=connection.id,
|
||||
server=connection.provider,
|
||||
account=connection.account,
|
||||
enabled=connection.enabled,
|
||||
metadata=metadata,
|
||||
)
|
||||
|
||||
@@ -38,6 +38,8 @@ class McpSdkAdapter(BackendAdapter):
|
||||
auth: AuthRecord | None,
|
||||
):
|
||||
transport = connection.transport
|
||||
if transport is None:
|
||||
raise ValueError(f"connection {connection.id!r} requires metadata.transport")
|
||||
if isinstance(transport, StdioSourceTransport):
|
||||
auth_env = mcp_auth_env(auth)
|
||||
env = dict(transport.env)
|
||||
@@ -67,7 +69,7 @@ class McpSdkAdapter(BackendAdapter):
|
||||
yield session
|
||||
return
|
||||
|
||||
raise ValueError(f"unsupported MCP transport {transport.kind!r}")
|
||||
raise ValueError(f"unsupported MCP transport {type(transport).__name__}")
|
||||
|
||||
async def list_tools(
|
||||
self,
|
||||
@@ -101,9 +103,10 @@ class McpSdkAdapter(BackendAdapter):
|
||||
connection: McpSourceConnection,
|
||||
auth: AuthRecord | None,
|
||||
) -> dict[str, Any]:
|
||||
transport = connection.transport
|
||||
return {
|
||||
"server": connection.provider,
|
||||
"transport": connection.transport.kind,
|
||||
"transport": transport.kind if transport is not None else None,
|
||||
}
|
||||
|
||||
async def read_resource(
|
||||
|
||||
@@ -39,7 +39,7 @@ class McpSourceConnection:
|
||||
id: str
|
||||
provider: str
|
||||
account: str
|
||||
transport: SourceTransport
|
||||
transport: SourceTransport | None = None
|
||||
enabled: bool = True
|
||||
profile: str | None = None
|
||||
auth_ref: str | None = None
|
||||
@@ -56,6 +56,16 @@ class McpSourceConnection:
|
||||
"MCP source connection id must match provider/account fields"
|
||||
)
|
||||
|
||||
@property
|
||||
def server(self) -> str:
|
||||
"""Compatibility alias for older adapter code.
|
||||
|
||||
`provider` is the source-provider term. The old broker DTO called the
|
||||
same field `server`, and several fake/custom adapters still read it.
|
||||
"""
|
||||
|
||||
return self.provider
|
||||
|
||||
|
||||
def mcp_source_connection_from_registry_entry(
|
||||
entry: McpSourceRegistryEntry,
|
||||
@@ -103,7 +113,9 @@ def mcp_source_connection_from_connection_config(
|
||||
)
|
||||
|
||||
|
||||
def _transport_from_connection_metadata(connection: ConnectionConfig) -> SourceTransport:
|
||||
def _transport_from_connection_metadata(
|
||||
connection: ConnectionConfig,
|
||||
) -> SourceTransport | None:
|
||||
transport = connection.metadata.get("transport")
|
||||
if isinstance(transport, dict):
|
||||
kind = transport.get("kind")
|
||||
@@ -123,6 +135,11 @@ def _transport_from_connection_metadata(connection: ConnectionConfig) -> SourceT
|
||||
str(key): str(value)
|
||||
for key, value in dict(connection.metadata.get("env", {})).items()
|
||||
},
|
||||
cwd=(
|
||||
str(connection.metadata["cwd"])
|
||||
if connection.metadata.get("cwd") is not None
|
||||
else None
|
||||
),
|
||||
)
|
||||
if transport in _FLAT_HTTP_TRANSPORTS:
|
||||
url = connection.metadata.get("url", "")
|
||||
@@ -138,7 +155,7 @@ def _transport_from_connection_metadata(connection: ConnectionConfig) -> SourceT
|
||||
raise ValueError(
|
||||
f"connection {connection.id!r} has unrecognized metadata.transport {transport!r}"
|
||||
)
|
||||
raise ValueError(f"connection {connection.id!r} requires metadata.transport")
|
||||
return None
|
||||
|
||||
|
||||
__all__ = [
|
||||
|
||||
@@ -147,6 +147,7 @@ def connection_config_to_registry_entry(
|
||||
"command": connection.metadata.get("command", ""),
|
||||
"args": list(connection.metadata.get("args", [])),
|
||||
"env": dict(connection.metadata.get("env", {})),
|
||||
"cwd": connection.metadata.get("cwd"),
|
||||
}
|
||||
elif transport in _FLAT_HTTP_TRANSPORTS:
|
||||
legacy_transport_value = transport
|
||||
|
||||
@@ -12,6 +12,7 @@ class StdioSourceTransport(SourceRegistryBaseModel):
|
||||
command: str = Field(min_length=1)
|
||||
args: tuple[str, ...] = ()
|
||||
env: dict[str, str] = Field(default_factory=dict)
|
||||
cwd: str | None = None
|
||||
|
||||
|
||||
class HttpSourceTransport(SourceRegistryBaseModel):
|
||||
|
||||
Reference in New Issue
Block a user