connect method

Connect to an MCP server.

Implementation

Future<McpServerConnection> connect(McpServerConfig config) async {
  final name = config.name;

  // Check if already connected
  final existing = _connections[name];
  if (existing is ConnectedMcpServer) return existing;

  _connections[name] = PendingMcpServer(serverName: name, config: config);

  try {
    final connection = await _connectTransport(config);
    _connections[name] = connection;

    // Register tools from this server
    if (connection is ConnectedMcpServer) {
      _registerMcpTools(connection);
    }

    return connection;
  } catch (e) {
    final failed = FailedMcpServer(
      serverName: name,
      config: config,
      error: e.toString(),
    );
    _connections[name] = failed;
    return failed;
  }
}