diff --git a/capiscio_mcp/integrations/mcp.py b/capiscio_mcp/integrations/mcp.py index 643fab0..943dd65 100644 --- a/capiscio_mcp/integrations/mcp.py +++ b/capiscio_mcp/integrations/mcp.py @@ -417,6 +417,29 @@ async def place_order(sku: str, quantity: int) -> str: identity = MCPServerIdentity.from_env_sync() return cls(identity=identity, **kwargs) + @classmethod + async def aconnect(cls, **kwargs: Any) -> "CapiscioMCPServer": + """Async variant of :meth:`connect` for use in running event loops. + + Unlike :meth:`connect`, which uses ``from_env_sync()`` and fails when + called from an already-running event loop (Jupyter, FastAPI, FastMCP), + this method uses the async ``MCPServerIdentity.from_env()`` directly. + + Example:: + + server = await CapiscioMCPServer.aconnect() + + @server.tool(min_trust_level=1) + async def my_tool(x: int) -> str: + return f"Result: {x}" + + server.run() + """ + from capiscio_mcp.connect import MCPServerIdentity + + identity = await MCPServerIdentity.from_env() + return cls(identity=identity, **kwargs) + # ------------------------------------------------------------------ # Private helpers # ------------------------------------------------------------------