|
1 | 1 | import os |
2 | | -import logging |
3 | 2 | from importlib.metadata import version |
4 | 3 | from contextlib import asynccontextmanager |
5 | 4 | from collections.abc import AsyncIterator |
6 | 5 | import mcp.types as types |
7 | 6 | import iris as irisnative |
8 | | -from mcp_server_iris.mcpserver import MCPServer, Context |
| 7 | +from mcp_server_iris.mcpserver import MCPServer, Context, logger |
9 | 8 | from mcp_server_iris.interoperability import init as interoperability |
10 | 9 |
|
11 | | -logger = logging.getLogger("mcp_server_iris") |
12 | 10 | logger.info("Starting InterSystems IRIS MCP Server") |
13 | 11 |
|
14 | 12 |
|
15 | 13 | def get_db_config(): |
16 | 14 | """Get database configuration from environment variables.""" |
17 | 15 | config = { |
18 | | - "hostname": os.getenv("IRIS_HOSTNAME", "localhost"), |
| 16 | + "hostname": os.getenv("IRIS_HOSTNAME"), |
19 | 17 | "port": int(os.getenv("IRIS_PORT", 1972)), |
20 | | - "namespace": os.getenv("IRIS_NAMESPACE", "USER"), |
21 | | - "username": os.getenv("IRIS_USERNAME", "_SYSTEM"), |
22 | | - "password": os.getenv("IRIS_PASSWORD", "SYS"), |
| 18 | + "namespace": os.getenv("IRIS_NAMESPACE"), |
| 19 | + "username": os.getenv("IRIS_USERNAME"), |
| 20 | + "password": os.getenv("IRIS_PASSWORD"), |
23 | 21 | } |
24 | 22 |
|
25 | | - if not all([config["username"], config["password"], config["namespace"]]): |
| 23 | + if not all([config["hostname"], config["username"], config["password"], config["namespace"]]): |
26 | 24 | raise ValueError("Missing required database configuration") |
| 25 | + logger.info(f"Server configuration: iris://{config["username"]}:{"x"*8}@{config["hostname"]}:{config["port"]}/{config["namespace"]}") |
27 | 26 |
|
28 | 27 | return config |
29 | 28 |
|
30 | 29 |
|
31 | 30 | @asynccontextmanager |
32 | 31 | async def server_lifespan(server: MCPServer) -> AsyncIterator[dict]: |
33 | 32 | """Manage server startup and shutdown lifecycle.""" |
34 | | - config = get_db_config() |
35 | 33 | try: |
36 | | - db = irisnative.connect(**config) |
| 34 | + config = get_db_config() |
| 35 | + except ValueError: |
| 36 | + yield {"db": None, "iris": None} |
| 37 | + return |
| 38 | + try: |
| 39 | + |
| 40 | + db = irisnative.connect(sharedmemory=False, **config) |
37 | 41 | iris = irisnative.createIRIS(db) |
38 | 42 | yield {"db": db, "iris": iris} |
39 | | - except Exception: |
| 43 | + except Exception as ex: |
| 44 | + logger.error(f"Error connecting to IRIS: {ex}") |
40 | 45 | db = None |
41 | 46 | iris = None |
42 | 47 | yield {"db": db, "iris": iris} |
@@ -189,7 +194,10 @@ def main(): |
189 | 194 | args = parser.parse_args() |
190 | 195 | server.settings.port = args.port |
191 | 196 | server.settings.debug = args.debug |
192 | | - server.run(transport=args.transport) |
| 197 | + try: |
| 198 | + server.run(transport=args.transport) |
| 199 | + except KeyboardInterrupt: |
| 200 | + logger.info("Server stopped by user") |
193 | 201 |
|
194 | 202 |
|
195 | 203 | if __name__ == "__main__": |
|
0 commit comments