fix: accept flat source registry transports
This commit is contained in:
@@ -24,6 +24,20 @@ from .shared.names import RESERVED_CONNECTION_IDS
|
|||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .models import ConnectionConfig
|
from .models import ConnectionConfig
|
||||||
|
|
||||||
|
_FLAT_HTTP_TRANSPORTS = {"http", "streamable-http", "streamable_http", "sse"}
|
||||||
|
_TRANSPORT_METADATA_KEYS = {
|
||||||
|
"transport",
|
||||||
|
"command",
|
||||||
|
"args",
|
||||||
|
"env",
|
||||||
|
"cwd",
|
||||||
|
"url",
|
||||||
|
"headers",
|
||||||
|
"profile",
|
||||||
|
"auth_ref",
|
||||||
|
"source_registry",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class StdioSourceTransport(SourceRegistryBaseModel):
|
class StdioSourceTransport(SourceRegistryBaseModel):
|
||||||
kind: Literal["stdio"] = "stdio"
|
kind: Literal["stdio"] = "stdio"
|
||||||
@@ -146,7 +160,8 @@ def connection_config_to_registry_entry(
|
|||||||
"args": list(connection.metadata.get("args", [])),
|
"args": list(connection.metadata.get("args", [])),
|
||||||
"env": dict(connection.metadata.get("env", {})),
|
"env": dict(connection.metadata.get("env", {})),
|
||||||
}
|
}
|
||||||
elif transport == "streamable_http":
|
elif transport in _FLAT_HTTP_TRANSPORTS:
|
||||||
|
legacy_transport = transport
|
||||||
transport = {
|
transport = {
|
||||||
"kind": "http",
|
"kind": "http",
|
||||||
"url": connection.metadata.get("url", ""),
|
"url": connection.metadata.get("url", ""),
|
||||||
@@ -162,7 +177,14 @@ def connection_config_to_registry_entry(
|
|||||||
)
|
)
|
||||||
profile = connection.metadata.get("profile")
|
profile = connection.metadata.get("profile")
|
||||||
auth_ref = connection.metadata.get("auth_ref")
|
auth_ref = connection.metadata.get("auth_ref")
|
||||||
return McpSourceRegistryEntry.model_validate(
|
source_metadata = {
|
||||||
|
key: value
|
||||||
|
for key, value in connection.metadata.items()
|
||||||
|
if key not in _TRANSPORT_METADATA_KEYS
|
||||||
|
}
|
||||||
|
if "legacy_transport" in locals():
|
||||||
|
source_metadata["legacy_transport"] = legacy_transport
|
||||||
|
entry = McpSourceRegistryEntry.model_validate(
|
||||||
{
|
{
|
||||||
"id": connection.id,
|
"id": connection.id,
|
||||||
"enabled": connection.enabled,
|
"enabled": connection.enabled,
|
||||||
@@ -171,13 +193,10 @@ def connection_config_to_registry_entry(
|
|||||||
"profile": profile if isinstance(profile, str) else None,
|
"profile": profile if isinstance(profile, str) else None,
|
||||||
"transport": transport,
|
"transport": transport,
|
||||||
"auth_ref": auth_ref if isinstance(auth_ref, str) else None,
|
"auth_ref": auth_ref if isinstance(auth_ref, str) else None,
|
||||||
"metadata": {
|
"metadata": source_metadata,
|
||||||
key: value
|
|
||||||
for key, value in connection.metadata.items()
|
|
||||||
if key not in {"transport", "profile", "auth_ref", "source_registry"}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
return entry
|
||||||
|
|
||||||
|
|
||||||
def workflow_mcp_source_to_connection_config(source: object) -> ConnectionConfig:
|
def workflow_mcp_source_to_connection_config(source: object) -> ConnectionConfig:
|
||||||
|
|||||||
@@ -146,6 +146,52 @@ def test_connection_config_to_registry_entry_preserves_transport_metadata() -> N
|
|||||||
assert entry.metadata["region"] == "us"
|
assert entry.metadata["region"] == "us"
|
||||||
|
|
||||||
|
|
||||||
|
def test_connection_config_to_registry_entry_accepts_flat_stdio_metadata() -> None:
|
||||||
|
connection = ConnectionConfig(
|
||||||
|
id="github.work",
|
||||||
|
server="github",
|
||||||
|
account="work",
|
||||||
|
metadata={
|
||||||
|
"transport": "stdio",
|
||||||
|
"command": "npx",
|
||||||
|
"args": ["server"],
|
||||||
|
"env": {"DEBUG": "1"},
|
||||||
|
"region": "us",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
entry = connection_config_to_registry_entry(connection)
|
||||||
|
|
||||||
|
assert entry.transport.kind == "stdio"
|
||||||
|
assert isinstance(entry.transport, StdioSourceTransport)
|
||||||
|
assert entry.transport.command == "npx"
|
||||||
|
assert entry.transport.args == ("server",)
|
||||||
|
assert entry.transport.env == {"DEBUG": "1"}
|
||||||
|
assert entry.metadata == {"region": "us"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_connection_config_to_registry_entry_accepts_flat_http_metadata() -> None:
|
||||||
|
connection = ConnectionConfig(
|
||||||
|
id="context7.default",
|
||||||
|
server="context7",
|
||||||
|
account="default",
|
||||||
|
metadata={
|
||||||
|
"transport": "sse",
|
||||||
|
"url": "http://127.0.0.1:3000/sse",
|
||||||
|
"headers": {"X-Test": "yes"},
|
||||||
|
"purpose": "legacy",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
entry = connection_config_to_registry_entry(connection)
|
||||||
|
|
||||||
|
assert entry.transport.kind == "http"
|
||||||
|
assert isinstance(entry.transport, HttpSourceTransport)
|
||||||
|
assert str(entry.transport.url) == "http://127.0.0.1:3000/sse"
|
||||||
|
assert entry.transport.headers == {"X-Test": "yes"}
|
||||||
|
assert entry.metadata == {"purpose": "legacy", "legacy_transport": "sse"}
|
||||||
|
|
||||||
|
|
||||||
def test_connection_config_to_registry_entry_requires_transport_metadata() -> None:
|
def test_connection_config_to_registry_entry_requires_transport_metadata() -> None:
|
||||||
connection = ConnectionConfig(id="github.work", server="github", account="work")
|
connection = ConnectionConfig(id="github.work", server="github", account="work")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user