Skip to content

Commit 2f890f4

Browse files
committed
keep start if no server
1 parent b0505d2 commit 2f890f4

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

src/mcp_server_iris/interoperability.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ async def interoperability_production_create(name: str, ctx: Context) -> bool:
6363
raise ValueError(
6464
"Production name must in format packagenamespace.productionname, where packagenamespace can have multiple parts separated by dots"
6565
)
66-
iris = ctx.request_context.lifespan_context["iris"]
66+
iris = ctx.iris
6767
with transaction(iris):
6868
prod = iris.classMethodObject(
6969
"%Dictionary.ClassDefinition", "%OpenId", name
@@ -86,7 +86,7 @@ async def interoperability_production_status(
8686
full_status: bool = False,
8787
) -> str:
8888
logger.info("Interoperability Production Status" + f": {name}" if name else "")
89-
iris = ctx.request_context.lifespan_context["iris"]
89+
iris = ctx.iris
9090
refname = IRISReference(iris)
9191
refname.setValue(name)
9292
refstatus = IRISReference(iris)
@@ -123,7 +123,7 @@ async def interoperability_production_start(
123123
logger.info(
124124
"Starting Interoperability Production" + f": {name}" if name else "."
125125
)
126-
iris = ctx.request_context.lifespan_context["iris"]
126+
iris = ctx.iris
127127
raise_on_error(
128128
iris,
129129
iris.classMethodString(
@@ -152,7 +152,7 @@ async def interoperability_production_stop(
152152
force: bool = False,
153153
) -> str:
154154
logger.info("Sopping Interoperability Production.")
155-
iris = ctx.request_context.lifespan_context["iris"]
155+
iris = ctx.iris
156156
raise_on_error(
157157
iris,
158158
iris.classMethodString(
@@ -166,7 +166,7 @@ async def interoperability_production_recover(
166166
ctx: Context,
167167
) -> str:
168168
logger.info("Recovering Interoperability Production")
169-
iris = ctx.request_context.lifespan_context["iris"]
169+
iris = ctx.iris
170170
raise_on_error(
171171
iris, iris.classMethodString("Ens.Director", "RecoverProduction")
172172
)
@@ -177,7 +177,7 @@ async def interoperability_production_needsupdate(
177177
ctx: Context,
178178
) -> str:
179179
logger.info("Checking if Interoperability Production needs update")
180-
iris = ctx.request_context.lifespan_context["iris"]
180+
iris = ctx.iris
181181
reason = IRISReference(iris)
182182
result = iris.classMethodBoolean(
183183
"Ens.Director", "ProductionNeedsUpdate", reason
@@ -192,7 +192,7 @@ async def interoperability_production_update(
192192
timeout: int = None,
193193
force: bool = False,
194194
) -> str:
195-
iris = ctx.request_context.lifespan_context["iris"]
195+
iris = ctx.iris
196196
raise_on_error(
197197
iris,
198198
iris.classMethodString("Ens.Director", "UpdateProduction", timeout, force),
@@ -215,7 +215,7 @@ async def interoperability_production_logs(
215215
log_type_alert and log_type.append(LogType.Alert.value)
216216
log_type_error and log_type.append(LogType.Error.value)
217217
log_type_warning and log_type.append(LogType.Warning.value)
218-
db = ctx.request_context.lifespan_context["db"]
218+
db = ctx.db
219219
with db.cursor() as cur:
220220
sql = f"""
221221
select top ? TimeLogged , %External(Type) Type, ConfigName, Text
@@ -236,7 +236,7 @@ async def interoperability_production_queues(
236236
ctx: Context,
237237
) -> str:
238238
queues = []
239-
db = ctx.request_context.lifespan_context["db"]
239+
db = ctx.db
240240
with db.cursor() as cur:
241241
sql = "select * from Ens.Queue_Enumerate()"
242242
cur.execute(sql)

src/mcp_server_iris/server.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ async def server_lifespan(server: MCPServer) -> AsyncIterator[dict]:
3535
db = irisnative.connect(**config)
3636
iris = irisnative.createIRIS(db)
3737
yield {"db": db, "iris": iris}
38+
except Exception:
39+
db = None
40+
iris = None
41+
yield {"db": db, "iris": iris}
3842
finally:
3943
if db:
4044
db.close()
@@ -148,7 +152,7 @@ async def execute_sql(
148152
) -> list[types.TextContent]:
149153
# params = arguments.get("params", [])
150154
logger.info(f"Executing SQL query: {query}")
151-
conn = ctx.request_context.lifespan_context["db"]
155+
conn = ctx.db
152156
with conn.cursor() as cursor:
153157
cursor.execute(query, params)
154158
# limit by 100 rows

src/mcp_server_iris/sql.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import logging
2+
import mcp.types as types
3+
from mcp_server_iris.mcpserver import MCPServer, Context
4+
5+
6+
def init(server: MCPServer, logger: logging.Logger) -> None:
7+
"""Initialize the SQL server with a tool to execute SQL queries."""
8+
logger.info("Initializing SQL tool for InterSystems IRIS MCP Server")
9+
10+
@server.tool(description="Execute an SQL query on the Server")
11+
async def execute_sql(
12+
query: str, ctx: Context, params: list[str] = []
13+
) -> list[types.TextContent]:
14+
# params = arguments.get("params", [])
15+
logger.info(f"Executing SQL query: {query}")
16+
conn = ctx.db
17+
with conn.cursor() as cursor:
18+
cursor.execute(query, params)
19+
# limit by 100 rows
20+
rows = cursor.fetchall()[:100]
21+
return [types.TextContent(type="text", text=str(rows))]

0 commit comments

Comments
 (0)