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

Function useConnection

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

184// ---------------------------------------------------------------------------
185
186const useConnection = (
187 connection: McpConnection,
188 toolName: string,
189 args: Record<string, unknown>,
190 elicit: Elicit,
191 onToolListChanged: (() => void) | undefined,
192): Effect.Effect<unknown, McpInvocationError | McpOAuthReauthorizationRequired> =>
193 Effect.gen(function* () {
194 installElicitationHandler(connection.client, elicit);
195 installToolListChangedHandler(connection.client, onToolListChanged);
196 return yield* Effect.tryPromise({
197 try: () => connection.client.callTool({ name: toolName, arguments: args }),
198 catch: (cause) => {
199 if (Predicate.isTagged(cause, "McpOAuthReauthorizationRequired")) {
200 return new McpOAuthReauthorizationRequired({
201 message: "MCP OAuth re-authorization required",
202 });
203 }
204 // Raised by the fetch adapter when the 403 carried an RFC 6750
205 // insufficient_scope challenge (the authProvider path, where the SDK
206 // would otherwise consume the challenge before we could see it).
207 if (Predicate.isTagged(cause, "McpInsufficientScopeError")) {
208 return new McpInvocationError({
209 toolName,
210 message: `MCP tool call failed for ${toolName}`,
211 status: 403,
212 insufficientScope: true,
213 transportFailure: true,
214 });
215 }
216 const status = httpStatusFromCause(cause);
217 const protocolFailure = asProtocolError(cause) !== undefined;
218 return new McpInvocationError({
219 toolName,
220 message: `MCP tool call failed for ${toolName}`,
221 ...(status === undefined ? {} : { status }),
222 ...(!protocolFailure ? { transportFailure: true } : {}),
223 ...(isUnknownToolCause(cause, toolName) ? { unknownTool: true } : {}),
224 ...(status === 403 && insufficientScopeFromCause(cause)
225 ? { insufficientScope: true }
226 : {}),
227 });
228 },
229 }).pipe(
230 Effect.withSpan("plugin.mcp.client.call_tool", {
231 attributes: { "mcp.tool.name": toolName },
232 }),
233 );
234 });
235
236// ---------------------------------------------------------------------------
237// Public API

Callers 1

useFunction · 0.85

Calls 6

httpStatusFromCauseFunction · 0.90
asProtocolErrorFunction · 0.85
isUnknownToolCauseFunction · 0.85

Tested by

no test coverage detected