MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / connectClient

Function connectClient

packages/plugins/mcp/src/sdk/connection.ts:503–545  ·  view source on GitHub ↗
(input: {
  transport: string;
  createTransport: (sdk: McpClientSdk) => Parameters<Client["connect"]>[0];
  versionNegotiation?: { readonly mode: "auto" };
})

Source from the content-addressed store, hash-verified

501 });
502
503const connectClient = (input: {
504 transport: string;
505 createTransport: (sdk: McpClientSdk) => Parameters<Client["connect"]>[0];
506 versionNegotiation?: { readonly mode: "auto" };
507}): Effect.Effect<McpConnection, McpConnectionError | McpOAuthReauthorizationRequired> =>
508 Effect.gen(function* () {
509 const sdk = yield* Effect.tryPromise({
510 try: () => loadMcpClientSdk(),
511 catch: () =>
512 new McpConnectionError({
513 transport: input.transport,
514 message: "Failed to load MCP client module",
515 }),
516 });
517 const client = createClient(sdk, input.versionNegotiation);
518 const transportInstance = input.createTransport(sdk);
519
520 yield* Effect.tryPromise({
521 // Interruption (an HTTP 499 cancelling a health check, the discovery
522 // timeout) aborts this signal; the SDK then fails the in-flight
523 // handshake and closes the transport. Without it the abandoned connect
524 // kept the spawned stdio child alive forever; `docker run -i --rm`
525 // integrations stranded a container per interrupted dial (#1631).
526 try: (signal) => client.connect(transportInstance, { signal }),
527 catch: (cause) =>
528 connectionFailure(input.transport, `Failed connecting via ${input.transport}`, cause),
529 }).pipe(
530 // The negotiated era ("modern" = 2026-07-28 server/discover, "legacy" =
531 // 2025 initialize) is otherwise invisible: both eras list and call tools
532 // identically, so traces are the one place an integration author can
533 // verify which handshake a connection actually used.
534 Effect.tap(() =>
535 Effect.annotateCurrentSpan({
536 "plugin.mcp.protocol_era": client.getProtocolEra() ?? "unknown",
537 }),
538 ),
539 Effect.withSpan("plugin.mcp.connection.handshake", {
540 attributes: { "plugin.mcp.transport": input.transport },
541 }),
542 );
543
544 return connectionFromClient(client);
545 });
546
547// ---------------------------------------------------------------------------
548// Public factory

Callers 1

createMcpConnectorFunction · 0.70

Calls 5

loadMcpClientSdkFunction · 0.90
connectionFailureFunction · 0.85
connectionFromClientFunction · 0.85
createClientFunction · 0.70
connectMethod · 0.65

Tested by

no test coverage detected