Skip to content

Support named databases #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions integrations/mcp-memgraph/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ docker run --rm \
mcp-memgraph:latest
```

### Docker environment variables
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we are adding them to the README, which we should have done previously, can you add them to a separate section called configuration, and then Envs as a subtitle, since these envs also work without Docker?


- `MEMGRAPH_URL`: The Bolt URL of the Memgraph instance to connect to. Default: `bolt://host.docker.internal:7687`
- The default value allows you to connect to a Memgraph instance running on your host machine from within the Docker container.
- `MEMGRAPH_USER`: The username for authentication. Default: `memgraph`
- `MEMGRAPH_PASSWORD`: The password for authentication. Default: empty
- `MEMGRAPH_DATABASE`: The database name to connect to. Default: `memgraph`
- `MCP_TRANSPORT`: The transport protocol to use. Options: `http` (default), `stdio`

### Connecting from VS Code (HTTP server)

If you are using VS Code MCP extension or similar, your configuration for an HTTP server would look like:
Expand Down
4 changes: 3 additions & 1 deletion integrations/mcp-memgraph/src/mcp_memgraph/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@
MEMGRAPH_URL = os.environ.get("MEMGRAPH_URL", "bolt://localhost:7687")
MEMGRAPH_USER = os.environ.get("MEMGRAPH_USER", "")
MEMGRAPH_PASSWORD = os.environ.get("MEMGRAPH_PASSWORD", "")
MEMGRAPH_DATABASE = os.environ.get("MEMGRAPH_DATABASE", "memgraph")

logger.info(f"Connecting to Memgraph at {MEMGRAPH_URL} with user '{MEMGRAPH_USER}'")
logger.info(f"Connecting to Memgraph db '{MEMGRAPH_DATABASE}' at {MEMGRAPH_URL} with user '{MEMGRAPH_USER}'")

# Initialize Memgraph client
db = Memgraph(
url=MEMGRAPH_URL,
username=MEMGRAPH_USER,
password=MEMGRAPH_PASSWORD,
database=MEMGRAPH_DATABASE,
)


Expand Down
9 changes: 8 additions & 1 deletion memgraph-toolbox/src/memgraph_toolbox/api/memgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def __init__(
url: str = None,
username: str = None,
password: str = None,
database: str = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect this to live inside the driver configuration and not be visible inside the driver signature, as you have assumed.

Maybe we can group the ENVS that are driver specific, there is a lot of configuration on that side, so we do not continue to expand this signature, and instead just unpack the ENVS in MCP and pass the dictionary. What do you think?

driver_config: Optional[Dict] = None,
):
"""
Expand All @@ -22,11 +23,13 @@ def __init__(
- MEMGRAPH_URL (default: "bolt://localhost:7687")
- MEMGRAPH_USER (default: "")
- MEMGRAPH_PASSWORD (default: "")
- MEMGRAPH_DATABASE (default: "memgraph")

Args:
url: The Memgraph connection URL
username: Username for authentication
password: Password for authentication
database: The database name to connect to (default: "memgraph")
driver_config: Additional Neo4j driver configuration
"""

Expand All @@ -38,6 +41,9 @@ def __init__(
self.driver = GraphDatabase.driver(
url, auth=(username, password), **(driver_config or {})
)

self.database = database or os.environ.get("MEMGRAPH_DATABASE", "memgraph")

try:
import neo4j
except ImportError:
Expand Down Expand Up @@ -74,6 +80,7 @@ def query(self, query: str, params: dict = {}) -> List[Dict[str, Any]]:
data, _, _ = self.driver.execute_query(
query,
parameters_=params,
database_=self.database,
)
json_data = [r.data() for r in data]
return json_data
Expand Down Expand Up @@ -106,7 +113,7 @@ def query(self, query: str, params: dict = {}) -> List[Dict[str, Any]]:
raise

# fallback to allow implicit transactions
with self.driver.session() as session:
with self.driver.session(database=self.database) as session:
data = session.run(query, params)
json_data = [r.data() for r in data]
return json_data
Expand Down