Skip to content

Commit 0e4673b

Browse files
authored
modify graph schema query
Modified the existing query to match output style but include the relationship properties.
1 parent d6a0a39 commit 0e4673b

File tree

1 file changed

+67
-6
lines changed
  • servers/mcp-neo4j-cypher/src/mcp_neo4j_cypher

1 file changed

+67
-6
lines changed

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

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,73 @@ async def get_neo4j_schema() -> list[types.TextContent]:
5959
"""
6060

6161
get_schema_query = """
62-
call apoc.meta.data() yield label, property, type, other, unique, index, elementType
63-
where elementType = 'node' and not label starts with '_'
64-
with label,
65-
collect(case when type <> 'RELATIONSHIP' then [property, type + case when unique then " unique" else "" end + case when index then " indexed" else "" end] end) as attributes,
66-
collect(case when type = 'RELATIONSHIP' then [property, head(other)] end) as relationships
67-
RETURN label, apoc.map.fromPairs(attributes) as attributes, apoc.map.fromPairs(relationships) as relationships
62+
CALL apoc.meta.data()
63+
YIELD label, property, type, other, unique, index, elementType
64+
65+
// 1) collect all rows
66+
WITH collect({label:label,
67+
property:property,
68+
type:type,
69+
other:other,
70+
unique:unique,
71+
index:index,
72+
elementType:elementType}) AS meta
73+
74+
// 2) extract each node‐label exactly once
75+
WITH meta,
76+
apoc.coll.toSet([
77+
row IN meta
78+
WHERE row.elementType='node'
79+
AND NOT row.label STARTS WITH '_'
80+
| row.label
81+
]) AS labels
82+
83+
UNWIND labels AS label
84+
85+
// 3) build node attributes
86+
WITH meta, label,
87+
[row IN meta
88+
WHERE row.elementType='node'
89+
AND row.label = label
90+
AND row.type <> 'RELATIONSHIP'
91+
| [ row.property,
92+
row.type
93+
+ CASE WHEN row.unique THEN ' unique' ELSE '' END
94+
+ CASE WHEN row.index THEN ' indexed' ELSE '' END
95+
]
96+
] AS attributes,
97+
98+
// 4) collect the outgoing‐rel “stubs”
99+
[row IN meta
100+
WHERE row.elementType='node'
101+
AND row.label = label
102+
AND row.type = 'RELATIONSHIP'
103+
| row
104+
] AS relRows
105+
106+
// 5) for each rel stub, look up its target and its own props
107+
WITH label, attributes,
108+
[ r IN relRows |
109+
[ r.property,
110+
{
111+
target: head(r.other),
112+
properties: apoc.map.fromPairs(
113+
[ rr IN meta
114+
WHERE rr.elementType = 'relationship'
115+
AND rr.label = r.property
116+
AND rr.property <> '...' // filter out any spurious meta-rows if needed
117+
| [ rr.property, rr.type ]
118+
]
119+
)
120+
}
121+
]
122+
] AS relationships
123+
124+
RETURN
125+
label,
126+
apoc.map.fromPairs(attributes) AS attributes,
127+
apoc.map.fromPairs(relationships) AS relationships;
128+
68129
"""
69130

70131
try:

0 commit comments

Comments
 (0)