Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions scripts/generate_mcp_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@

AGENT_VIEWS = {
"getAnalytics": "summary",
"listAds": "summary",
"listAdSets": "summary",
}

AGENT_DEPTHS = {
"getAdTree": "adset",
}

SAME_ARGUMENTS_IDEMPOTENT_OPERATIONS = {
Expand Down Expand Up @@ -437,24 +443,35 @@ def _with_page_size(operation_id: str, params: list[dict[str, Any]]) -> list[dic
]


def _with_agent_view(operation_id: str, params: list[dict[str, Any]]) -> list[dict[str, Any]]:
view = AGENT_VIEWS.get(operation_id)
if view is None:
def _with_agent_default(
operation_id: str,
params: list[dict[str, Any]],
*,
name: str,
value: str | None,
widest: str,
) -> list[dict[str, Any]]:
if value is None:
return params
if not any(p["name"] == "view" for p in params):
print(f"Warning: {operation_id} has no `view` parameter; its agent default is not applied.", file=sys.stderr)
if not any(p["name"] == name for p in params):
print(f"Warning: {operation_id} has no `{name}` parameter; its agent default is not applied.", file=sys.stderr)
return [
{
**p,
"default": f'"{view}"',
"description": f"{p['description'].strip()} Defaults to {view} here; pass full for every field.".strip(),
"default": f'"{value}"',
"description": f"{p['description'].strip()} Defaults to {value} here; pass {widest} for everything.".strip(),
}
if p["name"] == "view"
if p["name"] == name
else p
for p in params
]


def _with_agent_view(operation_id: str, params: list[dict[str, Any]]) -> list[dict[str, Any]]:
params = _with_agent_default(operation_id, params, name="view", value=AGENT_VIEWS.get(operation_id), widest="full")
return _with_agent_default(operation_id, params, name="depth", value=AGENT_DEPTHS.get(operation_id), widest="ad")


def extract_parameters(
operation: dict[str, Any],
spec: dict[str, Any] | None = None,
Expand Down
79 changes: 75 additions & 4 deletions tests/test_mcp_agent_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
{
"name": "view",
"in": "query",
"schema": {"type": "string", "enum": ["full", "summary"], "default": "full"},
"schema": {
"type": "string",
"enum": ["full", "summary"],
"default": "full",
},
"description": "full returns everything.",
}
],
Expand All @@ -24,10 +28,20 @@ def mcp_generator(monkeypatch: pytest.MonkeyPatch):
return importlib.import_module("generate_mcp_tools")


def test_analytics_listing_defaults_to_the_summary_view_for_agents(mcp_generator) -> None:
params = mcp_generator._with_agent_view("getAnalytics", mcp_generator.extract_parameters(ANALYTICS_OPERATION, {}))
def test_analytics_listing_defaults_to_the_summary_view_for_agents(
mcp_generator,
) -> None:
params = mcp_generator._with_agent_view(
"getAnalytics", mcp_generator.extract_parameters(ANALYTICS_OPERATION, {})
)
handler = mcp_generator.generate_tool_handler(
"analytics_get_analytics", "analytics", "get_analytics", "Get analytics", params, True, "Get analytics"
"analytics_get_analytics",
"analytics",
"get_analytics",
"Get analytics",
params,
True,
"Get analytics",
)

assert "view: Literal['full', 'summary'] = \"summary\"" in handler
Expand All @@ -42,3 +56,60 @@ def test_a_spec_without_the_view_parameter_warns_instead_of_silently_skipping(

assert params == []
assert "getAnalytics has no `view` parameter" in capsys.readouterr().err


def _operation(operation_id: str, name: str, values: list[str], default: str) -> dict:
return {
"operationId": operation_id,
"parameters": [
{
"name": name,
"in": "query",
"schema": {"type": "string", "enum": values, "default": default},
"description": "How much to return.",
}
],
}


@pytest.mark.parametrize("operation_id", ["listAds", "listAdSets"])
def test_ads_listings_default_to_the_summary_view_for_agents(
mcp_generator, operation_id: str
) -> None:
operation = _operation(operation_id, "view", ["full", "summary"], "full")
params = mcp_generator._with_agent_view(
operation_id, mcp_generator.extract_parameters(operation, {})
)

assert params[0]["default"] == '"summary"'
assert params[0]["description"].endswith(
"Defaults to summary here; pass full for everything."
)


def test_the_ads_tree_defaults_to_ad_set_depth_for_agents(mcp_generator) -> None:
operation = _operation("getAdTree", "depth", ["campaign", "adset", "ad"], "ad")
params = mcp_generator._with_agent_view(
"getAdTree", mcp_generator.extract_parameters(operation, {})
)
handler = mcp_generator.generate_tool_handler(
"ads_get_ad_tree",
"ads",
"get_ad_tree",
"Get ad tree",
params,
True,
"Get ad tree",
)

assert "depth: Literal['campaign', 'adset', 'ad'] = \"adset\"" in handler
assert "Defaults to adset here; pass ad for everything." in handler


def test_a_tree_spec_without_the_depth_parameter_warns_and_keeps_its_parameters(
mcp_generator, capsys: pytest.CaptureFixture[str]
) -> None:
other = {"name": "page", "description": "Page.", "default": "1"}

assert mcp_generator._with_agent_view("getAdTree", [other]) == [other]
assert "getAdTree has no `depth` parameter" in capsys.readouterr().err
Loading