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

Function useConnection

packages/plugins/mcp/src/sdk/invoke.ts:161–210  ·  view source on GitHub ↗
(
  connection: McpConnection,
  toolName: string,
  args: Record<string, unknown>,
  elicit: Elicit,
  onToolListChanged: (() => void) | undefined,
)

Source from the content-addressed store, hash-verified

159// ---------------------------------------------------------------------------
160
161const useConnection = (
162 connection: McpConnection,
163 toolName: string,
164 args: Record<string, unknown>,
165 elicit: Elicit,
166 onToolListChanged: (() => void) | undefined,
167): Effect.Effect<unknown, McpInvocationError | McpOAuthReauthorizationRequired> =>
168 Effect.gen(function* () {
169 installElicitationHandler(connection.client, elicit);
170 installToolListChangedHandler(connection.client, onToolListChanged);
171 return yield* Effect.tryPromise({
172 try: () => connection.client.callTool({ name: toolName, arguments: args }),
173 catch: (cause) => {
174 if (Predicate.isTagged(cause, "McpOAuthReauthorizationRequired")) {
175 return new McpOAuthReauthorizationRequired({
176 message: "MCP OAuth re-authorization required",
177 });
178 }
179 // Raised by the fetch adapter when the 403 carried an RFC 6750
180 // insufficient_scope challenge (the authProvider path, where the SDK
181 // would otherwise consume the challenge before we could see it).
182 if (Predicate.isTagged(cause, "McpInsufficientScopeError")) {
183 return new McpInvocationError({
184 toolName,
185 message: `MCP tool call failed for ${toolName}`,
186 status: 403,
187 insufficientScope: true,
188 transportFailure: true,
189 });
190 }
191 const status = httpStatusFromCause(cause);
192 // oxlint-disable-next-line executor/no-instanceof-tagged-error -- boundary: MCP SDK protocol failures are its McpError subclass; transport failures use other error shapes
193 const protocolFailure = cause instanceof McpError;
194 return new McpInvocationError({
195 toolName,
196 message: `MCP tool call failed for ${toolName}`,
197 ...(status === undefined ? {} : { status }),
198 ...(!protocolFailure ? { transportFailure: true } : {}),
199 ...(isUnknownToolCause(cause, toolName) ? { unknownTool: true } : {}),
200 ...(status === 403 && insufficientScopeFromCause(cause)
201 ? { insufficientScope: true }
202 : {}),
203 });
204 },
205 }).pipe(
206 Effect.withSpan("plugin.mcp.client.call_tool", {
207 attributes: { "mcp.tool.name": toolName },
208 }),
209 );
210 });
211
212// ---------------------------------------------------------------------------
213// Public API

Callers 1

useFunction · 0.85

Calls 5

httpStatusFromCauseFunction · 0.90
isUnknownToolCauseFunction · 0.85

Tested by

no test coverage detected