You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: asciidoc/courses/genai-mcp-build-custom-tools-python/modules/2-database-features/lessons/2c-add-neo4j-connection/lesson.adoc
+40-69Lines changed: 40 additions & 69 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ First, create a new directory for your MCP server, and use the `uv add` command
27
27
----
28
28
mkdir server
29
29
cd server
30
-
uv add neo4j python-dotenv
30
+
uv add mcp neo4j python-dotenv
31
31
----
32
32
33
33
@@ -119,20 +119,14 @@ Then define a new `FastMCP` server instance with the name [copy]#Movies GraphRAG
Now that we have our lifespan management set up, let's create a tool to return the number of nodes and relationships in the graph.
131
-
132
-
133
-
First, create a `graph_statistics` tool function, and pass the `Context` object as a parameter.
127
+
Now that we have our lifespan management set up, let's create a tool to return the number of nodes and relationships in the graph.
134
128
135
-
Use `ctx.request_context.lifespan_context` to access the driver and database from the lifespan context.
129
+
Create a `graph_statistics` tool function that uses `ctx.request_context.lifespan_context` to access the driver and database from the lifespan context, then executes a Cypher query to count nodes and relationships:
1. Configure the Codespaces ports (6274, 6277, and 8000) to be **Public**
206
+
2. Connect the Inspector using **Streamable HTTP** transport with the forwarded addresses
207
+
3. Use the **Tools** tab to run the `graph_statistics` tool
208
+
4. Verify it returns the node and relationship counts from your database
235
209
236
-
Once you've implemented the lifespan management:
210
+
link:/courses/genai-mcp-build-custom-tools-python/1-getting-started/5c-test-with-inspector/[See full Inspector setup instructions^] if you need detailed steps.
237
211
238
-
1. The server should start without errors
239
-
2. The `graph_statistics` tool should return node and relationship counts from your database
240
-
3. The server should cleanly shut down when stopped (no connection warnings)
241
212
242
-
read::My lifespan management is working![]
213
+
read::I have database statistics![]
243
214
244
215
245
216
[TIP]
@@ -263,8 +234,8 @@ In this challenge, you successfully added lifespan management to your MCP server
263
234
* **Environment variables** - Stored credentials in `.env` file and loaded them with `python-dotenv`
264
235
* **Lifespan function** - Created an async context manager to initialize and clean up the Neo4j driver
265
236
* **Context access** - Used `ctx.request_context.lifespan_context` to access the driver in tools
266
-
* **Connection testing** - Verified the connection works with a simple test tool
237
+
* **Connection testing** - Verified the connection works with the `graph_statistics` tool via the MCP Inspector
267
238
268
239
Your server now properly manages the Neo4j driver lifecycle, creating it once on startup and reusing it across all tool calls.
269
240
270
-
In the next lesson, you'll learn about the MCP Inspector and how to use it to test your server's tools and resources.
241
+
In the next lesson, you'll learn how to add more advanced database features to your MCP server.
0 commit comments