Skip to content

Build and run agents you can see, understand and trust.

fix(hub): rewrite and declare MCP registry URL template variables (#3810)

ChengFad286c31e4d6d
diff --git a/src/agentscope/app/hub/_mcp/_github_hub.py b/src/agentscope/app/hub/_mcp/_github_hub.py
index 1ea1cb76b..4936f676e 100644
--- a/src/agentscope/app/hub/_mcp/_github_hub.py
+++ b/src/agentscope/app/hub/_mcp/_github_hub.py
@@ -234,7 +234,33 @@ class GitHubMCPHub(MCPHubBase):
"is_secret": header.get("is_secret", True),
}
- config: dict = {"type": "http_mcp", "url": remote["url"]}
+ url = remote["url"]
+ # Rewrite URL placeholders for the renderer and declare inputs.
+ config: dict = {"type": "http_mcp", "url": _substitute(url)}
+ for name, spec in (remote.get("variables") or {}).items():
+ if not isinstance(spec, dict):
+ continue
+ existing = inputs.get(name, {})
+ # Keep header requirements while adding the URL metadata.
+ inputs[name] = {
+ "description": spec.get("description")
+ or existing.get("description", ""),
+ "is_required": spec.get("isRequired", True)
+ or existing.get("is_required", False),
+ "is_secret": spec.get("isSecret", False)
+ or existing.get("is_secret", False),
+ "choices": spec.get("choices"),
+ "default": spec.get("default"),
+ }
+ for match in _PLACEHOLDER.finditer(url):
+ inputs.setdefault(
+ match.group(1),
+ {
+ "description": "",
+ "is_required": True,
+ "is_secret": False,
+ },
+ )
if headers:
config["headers"] = headers
return config, inputs
diff --git a/tests/hub_github_test.py b/tests/hub_github_test.py
index 94bfcbb81..2bdecc387 100644
--- a/tests/hub_github_test.py
+++ b/tests/hub_github_test.py
@@ -2,7 +2,7 @@
"""GitHub MCP registry card-building test case, without any network."""
from unittest import TestCase
-from agentscope.app._service import render_mcp
+from agentscope.app._service import MCPRenderError, render_mcp
from agentscope.app.hub import GitHubMCPHub
@@ -65,6 +65,192 @@ class GitHubCardTest(TestCase):
"Bearer sk",
)
+ def test_url_template_variable_is_rewritten_and_declared(self) -> None:
+ """A ``{var}`` in the URL becomes ``${var}`` and an input."""
+ card = self._card(
+ {
+ "remotes": [
+ {
+ "type": "streamable-http",
+ "url": "https://{tenant_id}.analytics.example.com/mcp",
+ "variables": {
+ "tenant_id": {
+ "description": "Your tenant identifier",
+ "isRequired": True,
+ },
+ },
+ },
+ ],
+ },
+ )
+ self.assertEqual(
+ card.config_template.url,
+ "https://${tenant_id}.analytics.example.com/mcp",
+ )
+ self.assertEqual(card.auth, "inputs")
+ prop = card.inputs_schema["properties"]["tenant_id"]
+ self.assertEqual(prop["description"], "Your tenant identifier")
+ self.assertIn("tenant_id", card.inputs_schema["required"])
+
+ def test_url_variable_is_substituted_by_render_mcp(self) -> None:
+ """The rendered client points at the resolved endpoint."""
+ card = self._card(
+ {
+ "remotes": [
+ {
+ "type": "streamable-http",
+ "url": "https://{tenant_id}.analytics.example.com/mcp",
+ "variables": {
+ "tenant_id": {
+ "description": "Tenant",
+ "isRequired": True,
+ },
+ },
+ },
+ ],
+ },
+ )
+ client = render_mcp(card, {"tenant_id": "us-cell1"})
+ self.assertEqual(
+ client.mcp_config.url,
+ "https://us-cell1.analytics.example.com/mcp",
+ )
+
+ def test_variable_metadata_is_preserved(self) -> None:
+ """choices/default/isSecret/isRequired feed the install form."""
+ card = self._card(
+ {
+ "remotes": [
+ {
+ "type": "streamable-http",
+ "url": "https://api.example.com/{region}/mcp",
+ "variables": {
+ "region": {
+ "description": "Deployment region",
+ "isRequired": True,
+ "choices": ["us-east-1", "eu-west-1"],
+ "default": "us-east-1",
+ "isSecret": True,
+ },
+ },
+ },
+ ],
+ },
+ )
+ prop = card.inputs_schema["properties"]["region"]
+ self.assertEqual(prop["description"], "Deployment region")
+ self.assertEqual(prop["enum"], ["us-east-1", "eu-west-1"])
+ self.assertEqual(prop["default"], "us-east-1")
+ self.assertTrue(prop["writeOnly"])
+ self.assertEqual(prop["format"], "password")
+
+ def test_shared_url_and_header_variable_metadata(self) -> None:
+ """Shared inputs retain URL metadata and header requirements."""
+ for header_secret, url_secret in (
+ (False, False),
+ (True, False),
+ (False, True),
+ (True, True),
+ ):
+ with self.subTest(
+ header_secret=header_secret,
+ url_secret=url_secret,
+ ):
+ card = self._card(
+ {
+ "remotes": [
+ {
+ "url": "https://{region}.example.com/mcp",
+ "headers": [
+ {
+ "name": "X-Region",
+ "value": "{region}",
+ "is_secret": header_secret,
+ },
+ ],
+ "variables": {
+ "region": {
+ "description": "Deployment region",
+ "isRequired": False,
+ "isSecret": url_secret,
+ "choices": ["us", "eu"],
+ "default": "us",
+ },
+ },
+ },
+ ],
+ },
+ )
+ expected_property: dict = {
+ "type": "string",
+ "title": "region",
+ "description": "Deployment region",
+ "enum": ["us", "eu"],
+ "default": "us",
+ }
+ if header_secret or url_secret:
+ expected_property.update(
+ {"writeOnly": True, "format": "password"},
+ )
+ self.assertEqual(
+ card.inputs_schema,
+ {
+ "type": "object",
+ "properties": {"region": expected_property},
+ "required": ["region"],
+ },
+ )
+ client = render_mcp(card, {})
+ self.assertEqual(
+ {
+ "url": client.mcp_config.url,
+ "headers": client.mcp_config.headers,
+ },
+ {
+ "url": "https://us.example.com/mcp",
+ "headers": {"X-Region": "us"},
+ },
+ )
+ with self.assertRaisesRegex(MCPRenderError, "not one of"):
+ render_mcp(card, {"region": "unsupported-region"})
+
+ def test_placeholder_without_variables_still_declared(self) -> None:
+ """A bare ``{var}`` with no ``variables`` block is still resolved."""
+ card = self._card(
+ {
+ "remotes": [
+ {
+ "type": "streamable-http",
+ "url": "https://{host}.example.com/mcp",
+ },
+ ],
+ },
+ )
+ self.assertEqual(
+ card.config_template.url,
+ "https://${host}.example.com/mcp",
+ )
+ self.assertIn("host", card.inputs_schema["properties"])
+
+ def test_plain_url_unchanged(self) -> None:
+ """Control: a remote with no variables needs no install inputs."""
+ card = self._card(
+ {
+ "remotes": [
+ {
+ "type": "streamable-http",
+ "url": "https://analytics.example.com/mcp",
+ },
+ ],
+ },
+ )
+ self.assertEqual(
+ card.config_template.url,
+ "https://analytics.example.com/mcp",
+ )
+ self.assertEqual(card.auth, "none")
+ self.assertEqual(card.inputs_schema, {})
+
def test_package_becomes_a_stdio_config(self) -> None:
"""A runnable package is a local process, so stateful."""
card = self._card(