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

Function useConnection

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

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

Callers 1

invokeMcpToolFunction · 0.85

Calls 5

httpStatusFromCauseFunction · 0.90
isUnknownToolCauseFunction · 0.85

Tested by

no test coverage detected