Skip to content

Commit da8b436

Browse files
authored
memory - add detailed warnings and info for config (#137)
1 parent 693af75 commit da8b436

File tree

4 files changed

+442
-11
lines changed

4 files changed

+442
-11
lines changed

servers/mcp-neo4j-memory/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Add error handling to catch Neo4j specific errors and improve error responses
1414
* Implement `ToolError` class from FastMCP
1515
* Add tool annotations
16+
* Add clear warnings for config declaration via cli and env variables
1617

1718
## v0.2.0
1819

servers/mcp-neo4j-memory/src/mcp_neo4j_memory/__init__.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
from . import server
22
import asyncio
33
import argparse
4-
import os
4+
import logging
55

6+
from .utils import process_config
7+
8+
logger = logging.getLogger("mcp_neo4j_memory")
9+
logger.setLevel(logging.INFO)
610

711
def main():
812
"""Main entry point for the package."""
@@ -17,16 +21,8 @@ def main():
1721
parser.add_argument("--server-path", default=None, help="HTTP path (default: /mcp/)")
1822

1923
args = parser.parse_args()
20-
asyncio.run(server.main(
21-
args.db_url or os.getenv("NEO4J_URL") or os.getenv("NEO4J_URI", "bolt://localhost:7687"),
22-
args.username or os.getenv("NEO4J_USERNAME", "neo4j"),
23-
args.password or os.getenv("NEO4J_PASSWORD", "password"),
24-
args.database or os.getenv("NEO4J_DATABASE", "neo4j"),
25-
args.transport or os.getenv("NEO4J_TRANSPORT", "stdio"),
26-
args.server_host or os.getenv("NEO4J_MCP_SERVER_HOST", "127.0.0.1"),
27-
args.server_port or int(os.getenv("NEO4J_MCP_SERVER_PORT", "8000")),
28-
args.server_path or os.getenv("NEO4J_MCP_SERVER_PATH", "/mcp/"),
29-
))
24+
config = process_config(args)
25+
asyncio.run(server.main(**config))
3026

3127

3228
# Optionally expose other important items at package level
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import argparse
2+
import os
3+
import logging
4+
from typing import Union
5+
6+
logger = logging.getLogger("mcp_neo4j_memory")
7+
logger.setLevel(logging.INFO)
8+
9+
def process_config(args: argparse.Namespace) -> dict[str, Union[str, int, None]]:
10+
"""
11+
Process the command line arguments and environment variables to create a config dictionary.
12+
This may then be used as input to the main server function.
13+
If any value is not provided, then a warning is logged and a default value is used, if appropriate.
14+
15+
Parameters
16+
----------
17+
args : argparse.Namespace
18+
The command line arguments.
19+
20+
Returns
21+
-------
22+
config : dict[str, str]
23+
The configuration dictionary.
24+
"""
25+
26+
config = dict()
27+
28+
# parse uri
29+
if args.db_url is not None:
30+
config["neo4j_uri"] = args.db_url
31+
else:
32+
if os.getenv("NEO4J_URL") is not None:
33+
config["neo4j_uri"] = os.getenv("NEO4J_URL")
34+
else:
35+
if os.getenv("NEO4J_URI") is not None:
36+
config["neo4j_uri"] = os.getenv("NEO4J_URI")
37+
else:
38+
logger.warning("Warning: No Neo4j connection URL provided. Using default: bolt://localhost:7687")
39+
config["neo4j_uri"] = "bolt://localhost:7687"
40+
41+
# parse username
42+
if args.username is not None:
43+
config["neo4j_user"] = args.username
44+
else:
45+
if os.getenv("NEO4J_USERNAME") is not None:
46+
config["neo4j_user"] = os.getenv("NEO4J_USERNAME")
47+
else:
48+
logger.warning("Warning: No Neo4j username provided. Using default: neo4j")
49+
config["neo4j_user"] = "neo4j"
50+
51+
# parse password
52+
if args.password is not None:
53+
config["neo4j_password"] = args.password
54+
else:
55+
if os.getenv("NEO4J_PASSWORD") is not None:
56+
config["neo4j_password"] = os.getenv("NEO4J_PASSWORD")
57+
else:
58+
logger.warning("Warning: No Neo4j password provided. Using default: password")
59+
config["neo4j_password"] = "password"
60+
61+
# parse database
62+
if args.database is not None:
63+
config["neo4j_database"] = args.database
64+
else:
65+
if os.getenv("NEO4J_DATABASE") is not None:
66+
config["neo4j_database"] = os.getenv("NEO4J_DATABASE")
67+
else:
68+
logger.warning("Warning: No Neo4j database provided. Using default: neo4j")
69+
config["neo4j_database"] = "neo4j"
70+
71+
# parse transport
72+
if args.transport is not None:
73+
config["transport"] = args.transport
74+
else:
75+
if os.getenv("NEO4J_TRANSPORT") is not None:
76+
config["transport"] = os.getenv("NEO4J_TRANSPORT")
77+
else:
78+
logger.warning("Warning: No transport type provided. Using default: stdio")
79+
config["transport"] = "stdio"
80+
81+
# parse server host
82+
if args.server_host is not None:
83+
if config["transport"] == "stdio":
84+
logger.warning("Warning: Server host provided, but transport is `stdio`. The `server_host` argument will be set, but ignored.")
85+
config["host"] = args.server_host
86+
else:
87+
if os.getenv("NEO4J_MCP_SERVER_HOST") is not None:
88+
if config["transport"] == "stdio":
89+
logger.warning("Warning: Server host provided, but transport is `stdio`. The `NEO4J_MCP_SERVER_HOST` environment variable will be set, but ignored.")
90+
config["host"] = os.getenv("NEO4J_MCP_SERVER_HOST")
91+
elif config["transport"] != "stdio":
92+
logger.warning("Warning: No server host provided and transport is not `stdio`. Using default server host: 127.0.0.1")
93+
config["host"] = "127.0.0.1"
94+
else:
95+
logger.info("Info: No server host provided and transport is `stdio`. `server_host` will be None.")
96+
config["host"] = None
97+
98+
# parse server port
99+
if args.server_port is not None:
100+
if config["transport"] == "stdio":
101+
logger.warning("Warning: Server port provided, but transport is `stdio`. The `server_port` argument will be set, but ignored.")
102+
config["port"] = args.server_port
103+
else:
104+
if os.getenv("NEO4J_MCP_SERVER_PORT") is not None:
105+
if config["transport"] == "stdio":
106+
logger.warning("Warning: Server port provided, but transport is `stdio`. The `NEO4J_MCP_SERVER_PORT` environment variable will be set, but ignored.")
107+
config["port"] = int(os.getenv("NEO4J_MCP_SERVER_PORT"))
108+
elif config["transport"] != "stdio":
109+
logger.warning("Warning: No server port provided and transport is not `stdio`. Using default server port: 8000")
110+
config["port"] = 8000
111+
else:
112+
logger.info("Info: No server port provided and transport is `stdio`. `server_port` will be None.")
113+
config["port"] = None
114+
115+
# parse server path
116+
if args.server_path is not None:
117+
if config["transport"] == "stdio":
118+
logger.warning("Warning: Server path provided, but transport is `stdio`. The `server_path` argument will be set, but ignored.")
119+
config["path"] = args.server_path
120+
else:
121+
if os.getenv("NEO4J_MCP_SERVER_PATH") is not None:
122+
if config["transport"] == "stdio":
123+
logger.warning("Warning: Server path provided, but transport is `stdio`. The `NEO4J_MCP_SERVER_PATH` environment variable will be set, but ignored.")
124+
config["path"] = os.getenv("NEO4J_MCP_SERVER_PATH")
125+
elif config["transport"] != "stdio":
126+
logger.warning("Warning: No server path provided and transport is not `stdio`. Using default server path: /mcp/")
127+
config["path"] = "/mcp/"
128+
else:
129+
logger.info("Info: No server path provided and transport is `stdio`. `server_path` will be None.")
130+
config["path"] = None
131+
132+
return config

0 commit comments

Comments
 (0)