Skip to main content
Glama

get_connection_status

Check the current connection state of the MCP Test MCP server, returning detailed information about server status, transport type, connection duration, and usage statistics.

Instructions

Check the current MCP server connection state.

Returns detailed information about the active connection including server information, transport type, connection duration, and usage statistics.

Returns: Dictionary with connection status including: - success: Always True - connected: Boolean indicating if currently connected - connection: Full ConnectionState if connected, None otherwise - message: Human-readable status message - metadata: Request timing and connection duration info

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The core handler implementation for the 'get_connection_status' MCP tool. This async function retrieves the current connection state from ConnectionManager.get_status(), computes metadata like request time and connection duration, handles connected/disconnected cases, provides user-facing ctx updates, logs appropriately, and returns a standardized response dictionary. Error handling ensures graceful degradation even on unexpected exceptions.
    @mcp.tool
    async def get_connection_status(ctx: Context) -> dict[str, Any]:
        """Check the current MCP server connection state.
    
        Returns detailed information about the active connection including
        server information, transport type, connection duration, and usage
        statistics.
    
        Returns:
            Dictionary with connection status including:
            - success: Always True
            - connected: Boolean indicating if currently connected
            - connection: Full ConnectionState if connected, None otherwise
            - message: Human-readable status message
            - metadata: Request timing and connection duration info
        """
        start_time = time.perf_counter()
    
        try:
            state = ConnectionManager.get_status()
            connected = state is not None
    
            elapsed_ms = (time.perf_counter() - start_time) * 1000
    
            metadata: dict[str, Any] = {
                "request_time_ms": round(elapsed_ms, 2),
            }
    
            if connected and state:
                # Calculate connection duration
                if state.connected_at:
                    duration_seconds = (
                        time.perf_counter() - state.connected_at.timestamp()
                    )
                    metadata["connection_duration_seconds"] = round(duration_seconds, 2)
    
                message = f"Connected to {state.server_url}"
                connection_data = state.model_dump(mode="json")
    
                # User-facing debug update
                await ctx.debug(f"Connection status: connected to {state.server_url}")
                # Detailed technical log
                logger.debug(
                    "Connection status checked",
                    extra={
                        "connected": True,
                        "server_url": state.server_url,
                        "statistics": state.statistics,
                    },
                )
            else:
                message = "Not connected to any MCP server"
                connection_data = None
    
                # User-facing debug update
                await ctx.debug("Connection status: not connected")
                # Detailed technical log
                logger.debug("Connection status checked", extra={"connected": False})
    
            return {
                "success": True,
                "connected": connected,
                "connection": connection_data,
                "message": message,
                "metadata": metadata,
            }
    
        except Exception as e:
            # Status check should never fail, but handle gracefully
            elapsed_ms = (time.perf_counter() - start_time) * 1000
    
            # User-facing error update
            await ctx.error(f"Unexpected error checking connection status: {str(e)}")
            # Detailed technical log
            logger.exception("Unexpected error checking connection status")
    
            return {
                "success": True,  # Still return success with disconnected state
                "connected": False,
                "connection": None,
                "message": f"Unable to determine connection status: {str(e)}",
                "metadata": {
                    "request_time_ms": round(elapsed_ms, 2),
                    "error": str(e),
                },
            }
  • Registration of tools by importing the tools modules (including connection.py containing get_connection_status). The @mcp.tool decorators in the imported modules automatically register the tools with the FastMCP server instance. This import sequence is critical for tool availability.
    # Import tool modules to trigger decorator registration
    # This MUST happen after mcp instance is imported but before the server runs
    from .tools import connection, tools, resources, prompts, llm
  • Re-export of get_connection_status from connection.py module, making it available when importing from tools package. This facilitates the server.py import of the tools package.
    from .connection import connect_to_server, disconnect, get_connection_status
    from .tools import call_tool, list_tools
    
    __all__ = [
        "connect_to_server",
        "disconnect",
        "get_connection_status",
        "list_tools",
        "call_tool",
    ]

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/rdwj/mcp-test-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server