|
| 1 | +# Common |
| 2 | + |
| 3 | +Common utilities and middleware for MCP Neo4j servers. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +You should not have to manually install this package, however if you are receiving import errors you may try reinstalling with the following: |
| 8 | + |
| 9 | +```bash |
| 10 | +uv sync --reinstall-package common |
| 11 | +``` |
| 12 | + |
| 13 | +## Features |
| 14 | + |
| 15 | +This package provides three main categories of utilities: |
| 16 | + |
| 17 | +1. **Middleware** - Pre-configured middleware for CORS and trusted host protection |
| 18 | +2. **Config Processing** - Utilities for processing command-line arguments and environment variables |
| 19 | +3. **Namespace Formatting** - Utilities for consistent namespace handling in tool naming |
| 20 | + |
| 21 | +## Middleware |
| 22 | + |
| 23 | +The middleware module provides factory functions for creating pre-configured Starlette middleware instances commonly needed by remotely deployed MCP Neo4j servers. |
| 24 | + |
| 25 | +### CORS Middleware |
| 26 | + |
| 27 | +```python |
| 28 | +from common.middleware import create_cors_middleware |
| 29 | + |
| 30 | +# Create CORS middleware with allowed origins |
| 31 | +cors_middleware = create_cors_middleware( |
| 32 | + allow_origins=["https://example.com", "https://app.example.com"] |
| 33 | +) |
| 34 | +``` |
| 35 | + |
| 36 | +The CORS middleware is configured with: |
| 37 | +- **Methods**: `GET`, `POST` |
| 38 | +- **Headers**: All headers allowed (`*`) |
| 39 | +- **Origins**: Configurable list of allowed origins |
| 40 | + |
| 41 | +### Trusted Host Middleware |
| 42 | + |
| 43 | +```python |
| 44 | +from common.middleware import create_trusted_host_middleware |
| 45 | + |
| 46 | +# Create trusted host middleware for DNS rebinding protection |
| 47 | +trusted_host_middleware = create_trusted_host_middleware( |
| 48 | + allowed_hosts=["localhost", "127.0.0.1", "myapp.com"] |
| 49 | +) |
| 50 | +``` |
| 51 | + |
| 52 | +## Config Processing |
| 53 | + |
| 54 | +The `arg_processing` module provides utilities for processing configuration from both command-line arguments and environment variables, with sensible defaults and logging. |
| 55 | + |
| 56 | +### Available Processing Functions |
| 57 | + |
| 58 | +Each function follows the pattern: check command-line args first, then environment variables, then apply defaults with appropriate logging. |
| 59 | + |
| 60 | +```python |
| 61 | +from common.utils.arg_processing import ( |
| 62 | + process_db_url, |
| 63 | + process_username, |
| 64 | + process_password, |
| 65 | +) |
| 66 | +import argparse |
| 67 | + |
| 68 | +# Example usage |
| 69 | +args = argparse.Namespace() # Your parsed arguments |
| 70 | +db_url = process_db_url(args) |
| 71 | +username = process_username(args) |
| 72 | +password = process_password(args) |
| 73 | +``` |
| 74 | + |
| 75 | +### Configuration Priority |
| 76 | + |
| 77 | +1. **Command-line arguments** |
| 78 | +2. **Environment variables** |
| 79 | +3. **Default values** |
| 80 | + |
| 81 | +### Environment Variables |
| 82 | + |
| 83 | +| Function | Environment Variables | Default | |
| 84 | +|----------|----------------------|---------| |
| 85 | +| `process_db_url` | `NEO4J_URL`, `NEO4J_URI` | `bolt://localhost:7687` | |
| 86 | +| `process_username` | `NEO4J_USERNAME` | `neo4j` | |
| 87 | +| `process_password` | `NEO4J_PASSWORD` | `password` | |
| 88 | +| `process_database` | `NEO4J_DATABASE` | `neo4j` | |
| 89 | +| `process_namespace` | `NEO4J_NAMESPACE` | `""` (empty) | |
| 90 | +| `process_transport` | `NEO4J_TRANSPORT` | `stdio` | |
| 91 | +| `process_server_host` | `NEO4J_MCP_SERVER_HOST` | `127.0.0.1` (ignored if transport is `stdio`) | |
| 92 | +| `process_server_port` | `NEO4J_MCP_SERVER_PORT` | `8000` (ignored if transport is `stdio`) | |
| 93 | +| `process_server_path` | `NEO4J_MCP_SERVER_PATH` | `/mcp/` (ignored if transport is `stdio`) | |
| 94 | +| `process_allow_origins` | `NEO4J_MCP_SERVER_ALLOW_ORIGINS` | `[]` (empty list) | |
| 95 | +| `process_allowed_hosts` | `NEO4J_MCP_SERVER_ALLOWED_HOSTS` | `["localhost", "127.0.0.1"]` | |
| 96 | +| `process_token_limit` | `NEO4J_RESPONSE_TOKEN_LIMIT` | `None` | |
| 97 | +| `process_read_timeout` | `NEO4J_READ_TIMEOUT` | `30` | |
| 98 | + |
| 99 | +## Namespace Formatting |
| 100 | + |
| 101 | +The `namespace` module provides utilities for consistent namespace formatting in tool naming conventions. |
| 102 | + |
| 103 | +### format_namespace Function |
| 104 | + |
| 105 | +```python |
| 106 | +from common.utils.namespace import format_namespace |
| 107 | + |
| 108 | +# Ensures namespace ends with hyphen for tool naming |
| 109 | +formatted = format_namespace("myapp") # Returns: "myapp-" |
| 110 | +formatted = format_namespace("myapp-") # Returns: "myapp-" (no change) |
| 111 | +``` |
| 112 | + |
| 113 | +This ensures consistent tool naming across MCP Neo4j servers, where namespaced tools follow the pattern `namespace-toolname` and non-namespaced tools use just the tool name. |
0 commit comments