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

Function useConnection

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

211// ---------------------------------------------------------------------------
212
213const useConnection = (
214 connection: McpConnection,
215 toolName: string,
216 args: Record<string, unknown>,
217 elicit: Elicit,
218 onToolListChanged: (() => void) | undefined,
219): Effect.Effect<unknown, McpInvocationError | McpOAuthReauthorizationRequired> =>
220 Effect.gen(function* () {
221 installElicitationHandler(connection.client, elicit);
222 installToolListChangedHandler(connection.client, onToolListChanged);
223 return yield* Effect.tryPromise({
224 try: () => connection.client.callTool({ name: toolName, arguments: args }),
225 catch: (cause) => {
226 if (Predicate.isTagged(cause, "McpOAuthReauthorizationRequired")) {
227 return new McpOAuthReauthorizationRequired({
228 message: "MCP OAuth re-authorization required",
229 });
230 }
231 // Raised by the fetch adapter when the 403 carried an RFC 6750
232 // insufficient_scope challenge (the authProvider path, where the SDK
233 // would otherwise consume the challenge before we could see it).
234 if (Predicate.isTagged(cause, "McpInsufficientScopeError")) {
235 return new McpInvocationError({
236 toolName,
237 message: `MCP tool call failed for ${toolName}`,
238 status: 403,
239 insufficientScope: true,
240 transportFailure: true,
241 });
242 }
243 const status = httpStatusFromCause(cause);
244 const protocolFailure = asProtocolError(cause) !== undefined;
245 return new McpInvocationError({
246 toolName,
247 message: `MCP tool call failed for ${toolName}`,
248 ...(status === undefined ? {} : { status }),
249 ...(!protocolFailure ? { transportFailure: true } : {}),
250 ...(isUnknownToolCause(cause, toolName) ? { unknownTool: true } : {}),
251 ...(status === 403 && insufficientScopeFromCause(cause)
252 ? { insufficientScope: true }
253 : {}),
254 });
255 },
256 }).pipe(
257 Effect.withSpan("plugin.mcp.client.call_tool", {
258 attributes: { "mcp.tool.name": toolName },
259 }),
260 );
261 });
262
263// ---------------------------------------------------------------------------
264// Public API

Callers 1

useFunction · 0.85

Calls 6

httpStatusFromCauseFunction · 0.90
asProtocolErrorFunction · 0.85
isUnknownToolCauseFunction · 0.85

Tested by

no test coverage detected