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

Function useConnection

packages/plugins/mcp/src/sdk/invoke.ts:170–219  ·  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

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

Callers 1

useFunction · 0.85

Calls 5

httpStatusFromCauseFunction · 0.90
isUnknownToolCauseFunction · 0.85

Tested by

no test coverage detected