fix(mcp): reject unsupported export_graph formats instead of mislabeling JSON

_tool_export_graph fell through to json.dumps(kg) for any format outside
the RDF set, including values never declared in the tool's own inputSchema
enum. Nothing in this server validates tool-call args against inputSchema
before dispatch, so a typo'd or unsupported format (e.g. "yaml") silently
returned JSON data labeled with the wrong format and no error.

Validate against the declared format list up front and reuse the same
constant for the inputSchema enum so the two can't drift apart again.
This commit is contained in:
KaifAhmad1
2026-08-25 16:18:33 +05:30
parent 6dad69cdb4
commit e2fc76cea0
2 changed files with 23 additions and 1 deletions
+15
View File
@@ -70,6 +70,21 @@ class TestExportGraphTool(unittest.TestCase):
def test_progress_is_disabled_for_the_server_process(self):
self.assertEqual(os.environ.get("SEMANTICA_DISABLE_PROGRESS"), "1")
def test_unsupported_format_returns_error_not_mislabeled_json(self):
"""A format outside the declared enum (typo, unsupported value, or a
client that skips schema validation) must error, not silently return
JSON data mislabeled with the requested format string."""
result = mcp_server._tool_export_graph({"format": "yaml"})
self.assertIn("error", result)
self.assertIn("yaml", result["error"])
def test_export_graph_schema_enum_matches_handled_formats(self):
"""The tool's declared inputSchema enum must not drift from the set
of formats the handler actually accepts."""
tool = next(t for t in mcp_server.TOOLS if t["name"] == "export_graph")
schema_enum = set(tool["inputSchema"]["properties"]["format"]["enum"])
self.assertEqual(schema_enum, set(mcp_server._EXPORT_GRAPH_FORMATS))
if __name__ == "__main__":
unittest.main()