Skip to content

Commit 754d369

Browse files
committed
pass at cleaning up keys to reduce tokens
1 parent 4f1741b commit 754d369

File tree

1 file changed

+68
-1
lines changed
  • servers/mcp-neo4j-cypher/src/mcp_neo4j_cypher

1 file changed

+68
-1
lines changed

servers/mcp-neo4j-cypher/src/mcp_neo4j_cypher/server.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,69 @@ async def get_neo4j_schema() -> list[types.TextContent]:
6262
CALL apoc.meta.schema();
6363
"""
6464

65+
def clean_schema(schema: dict) -> dict:
66+
cleaned = {}
67+
68+
for key, entry in schema.items():
69+
70+
new_entry = {
71+
"type": entry["type"]
72+
}
73+
if "count" in entry:
74+
new_entry["count"] = entry["count"]
75+
76+
labels = entry.get("labels", [])
77+
if labels:
78+
new_entry["labels"] = labels
79+
80+
props = entry.get("properties", {})
81+
clean_props = {}
82+
for pname, pinfo in props.items():
83+
cp = {}
84+
if "indexed" in pinfo:
85+
cp["indexed"] = pinfo["indexed"]
86+
if "type" in pinfo:
87+
cp["type"] = pinfo["type"]
88+
if cp:
89+
clean_props[pname] = cp
90+
if clean_props:
91+
new_entry["properties"] = clean_props
92+
93+
if entry.get("relationships"):
94+
rels_out = {}
95+
for rel_name, rel in entry["relationships"].items():
96+
cr = {}
97+
if "direction" in rel:
98+
cr["direction"] = rel["direction"]
99+
# nested labels
100+
rlabels = rel.get("labels", [])
101+
if rlabels:
102+
cr["labels"] = rlabels
103+
# nested properties
104+
rprops = rel.get("properties", {})
105+
clean_rprops = {}
106+
for rpname, rpinfo in rprops.items():
107+
crp = {}
108+
if "indexed" in rpinfo:
109+
crp["indexed"] = rpinfo["indexed"]
110+
if "type" in rpinfo:
111+
crp["type"] = rpinfo["type"]
112+
if crp:
113+
clean_rprops[rpname] = crp
114+
if clean_rprops:
115+
cr["properties"] = clean_rprops
116+
117+
if cr:
118+
rels_out[rel_name] = cr
119+
120+
if rels_out:
121+
new_entry["relationships"] = rels_out
122+
123+
cleaned[key] = new_entry
124+
125+
return cleaned
126+
127+
65128
try:
66129
async with neo4j_driver.session(database=database) as session:
67130
results_json_str = await session.execute_read(
@@ -70,7 +133,11 @@ async def get_neo4j_schema() -> list[types.TextContent]:
70133

71134
logger.debug(f"Read query returned {len(results_json_str)} rows")
72135

73-
return [types.TextContent(type="text", text=results_json_str)]
136+
schema = json.loads(results_json_str)[0].get('value')
137+
schema_clean = clean_schema(schema)
138+
schema_clean_str = json.dumps(schema_clean)
139+
140+
return [types.TextContent(type="text", text=schema_clean_str)]
74141

75142
except Exception as e:
76143
logger.error(f"Database error retrieving schema: {e}")

0 commit comments

Comments
 (0)