11import os
2+ import asyncio
3+ import subprocess
24from typing import Any
35
46import pytest
57import pytest_asyncio
68from neo4j import AsyncGraphDatabase
79from testcontainers .neo4j import Neo4jContainer
810
11+
912from mcp_neo4j_cypher .server import create_mcp_server
1013
1114neo4j = (
@@ -69,3 +72,75 @@ def init_data(setup: Neo4jContainer, clear_data: Any):
6972def clear_data (setup : Neo4jContainer ):
7073 with setup .get_driver ().session (database = "neo4j" ) as session :
7174 session .run ("MATCH (n) DETACH DELETE n" )
75+
76+
77+
78+ @pytest_asyncio .fixture
79+ async def sse_server (setup : Neo4jContainer ):
80+ """Start the MCP server in SSE mode."""
81+
82+
83+ process = await asyncio .create_subprocess_exec (
84+ "uv" , "run" , "mcp-neo4j-cypher" ,
85+ "--transport" , "sse" ,
86+ "--server-host" , "127.0.0.1" ,
87+ "--server-port" , "8002" ,
88+ "--db-url" , setup .get_connection_url (),
89+ "--username" , setup .username ,
90+ "--password" , setup .password ,
91+ "--database" , "neo4j" ,
92+ stdout = subprocess .PIPE ,
93+ stderr = subprocess .PIPE ,
94+ cwd = os .getcwd ()
95+ )
96+
97+ await asyncio .sleep (3 )
98+
99+ if process .returncode is not None :
100+ stdout , stderr = await process .communicate ()
101+ raise RuntimeError (f"Server failed to start. stdout: { stdout .decode ()} , stderr: { stderr .decode ()} " )
102+
103+ yield process
104+
105+ try :
106+ process .terminate ()
107+ await asyncio .wait_for (process .wait (), timeout = 5.0 )
108+ except asyncio .TimeoutError :
109+ process .kill ()
110+ await process .wait ()
111+
112+ @pytest_asyncio .fixture
113+ async def http_server (setup : Neo4jContainer ):
114+ """Start the MCP server in HTTP mode."""
115+
116+ # Start server process in HTTP mode using the installed binary
117+ process = await asyncio .create_subprocess_exec (
118+ "uv" , "run" , "mcp-neo4j-cypher" ,
119+ "--transport" , "http" ,
120+ "--server-host" , "127.0.0.1" ,
121+ "--server-port" , "8001" ,
122+ "--db-url" , setup .get_connection_url (),
123+ "--username" , setup .username ,
124+ "--password" , setup .password ,
125+ stdout = subprocess .PIPE ,
126+ stderr = subprocess .PIPE ,
127+ cwd = os .getcwd ()
128+ )
129+
130+ # Wait for server to start
131+ await asyncio .sleep (3 )
132+
133+ # Check if process is still running
134+ if process .returncode is not None :
135+ stdout , stderr = await process .communicate ()
136+ raise RuntimeError (f"Server failed to start. stdout: { stdout .decode ()} , stderr: { stderr .decode ()} " )
137+
138+ yield process
139+
140+ # Cleanup
141+ try :
142+ process .terminate ()
143+ await asyncio .wait_for (process .wait (), timeout = 5.0 )
144+ except asyncio .TimeoutError :
145+ process .kill ()
146+ await process .wait ()
0 commit comments