fix(mcp): extract_relations tool crashes with missing entities arg

RelationExtractor.extract_relations(text, entities, ...) requires
entities, but the tool called it with only text, raising TypeError
on every invocation. Run NER first and pass the resulting entities
through, matching how the rest of the pipeline extracts relations.
This commit is contained in:
KaifAhmad1
2026-08-26 15:30:26 +05:30
parent 1452dab5fa
commit 84ccc7c0e3
2 changed files with 21 additions and 2 deletions
+5 -2
View File
@@ -127,13 +127,16 @@ def _tool_extract_relations(args: dict) -> dict:
text = args.get("text", "")
if not text:
return {"error": "text is required"}
from semantica.semantic_extract import RelationExtractor, TripletExtractor
from semantica.semantic_extract import NamedEntityRecognizer, RelationExtractor, TripletExtractor
rel_kwargs = {}
ner_kwargs = {}
for k in ("model", "language"):
if args.get(k) is not None:
rel_kwargs[k] = args[k]
ner_kwargs[k] = args[k]
method = args.get("method", "pattern")
relations = RelationExtractor(method=method, **rel_kwargs).extract_relations(text)
entities = NamedEntityRecognizer(methods=["ml"], **ner_kwargs).extract_entities(text) or []
relations = RelationExtractor(method=method, **rel_kwargs).extract_relations(text, entities)
triplets = TripletExtractor().extract_triplets(text)
return {
"relations": [
@@ -877,6 +877,22 @@ class TestEntityExtractionSurfaceText(unittest.TestCase):
result = _tool_extract_relations({})
self.assertIn("error", result)
def test_extract_relations_with_text_does_not_raise(self):
"""extract_relations must not raise TypeError for missing `entities`
(RelationExtractor.extract_relations requires an `entities` arg;
the tool must supply one, e.g. by running NER first)."""
from semantica.mcp_server import _tool_extract_relations
try:
result = _tool_extract_relations({"text": "Apple announced new iPhone"})
except Exception as exc:
self.fail(f"extract_relations raised unexpectedly: {exc!r}")
self.assertNotIn("error", result,
"extract_relations should not error on valid text input")
self.assertIn("relations", result)
self.assertIn("triplets", result)
# ---------------------------------------------------------------------------
# Part 13: query_graph node / search modes