Skip to content

Provide async connect() variant for running event loop contexts #35

@beonde

Description

@beonde

Problem

CapiscioMCPServer.connect() and the underlying MCPServerIdentity.from_env_sync() / connect_sync() use asyncio.run(), which raises RuntimeError: asyncio.run() cannot be called from a running event loop when invoked from:

  • Jupyter notebooks
  • ASGI frameworks (FastAPI, Starlette)
  • FastMCP setups that already have an event loop
  • Any async def entrypoint

This is a common usage pattern for MCP server bootstrapping code.

Proposed fix

  • Add CapiscioMCPServer.connect_async() (or make connect() async and provide connect_sync())
  • Alternatively, detect a running loop and use run_coroutine_threadsafe as other sync wrappers in the repo do
  • Document the limitation prominently in the meantime

Affected methods

  • CapiscioMCPServer.connect()integrations/mcp.py:418
  • MCPServerIdentity.from_env_sync()connect.py:687
  • MCPServerIdentity.connect_sync()connect.py:687

Context

Identified during PR #32 code review (Copilot review comments on connect.py:687 and mcp.py:418).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions