@github/copilot-sdk 1.0.4 → 1.0.5-preview.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/session.js CHANGED
@@ -27,11 +27,12 @@ class CopilotSession {
27
27
  * @param traceContextProvider - Optional callback to get W3C Trace Context for outbound RPCs
28
28
  * @internal This constructor is internal. Use {@link CopilotClient.createSession} to create sessions.
29
29
  */
30
- constructor(sessionId, connection, _workspacePath, traceContextProvider) {
30
+ constructor(sessionId, connection, _workspacePath, traceContextProvider, options) {
31
31
  this.sessionId = sessionId;
32
32
  this.connection = connection;
33
33
  this._workspacePath = _workspacePath;
34
34
  this.traceContextProvider = traceContextProvider;
35
+ this.mcpAuthHandler = options?.mcpAuthHandler;
35
36
  }
36
37
  sessionId;
37
38
  connection;
@@ -43,6 +44,7 @@ class CopilotSession {
43
44
  bearerTokenProviders = /* @__PURE__ */ new Map();
44
45
  commandHandlers = /* @__PURE__ */ new Map();
45
46
  permissionHandler;
47
+ mcpAuthHandler;
46
48
  userInputHandler;
47
49
  elicitationHandler;
48
50
  exitPlanModeHandler;
@@ -254,6 +256,18 @@ class CopilotSession {
254
256
  if (this.permissionHandler) {
255
257
  void this._executePermissionAndRespond(requestId, permissionRequest);
256
258
  }
259
+ } else if (event.type === "mcp.oauth_required") {
260
+ const data = event.data;
261
+ if (!data?.requestId) {
262
+ return;
263
+ }
264
+ if (!this.mcpAuthHandler) {
265
+ console.warn(
266
+ `Received MCP OAuth request without a registered MCP auth handler. SessionId=${this.sessionId}, RequestId=${data.requestId}`
267
+ );
268
+ return;
269
+ }
270
+ void this._executeMcpAuthAndRespond(data);
257
271
  } else if (event.type === "command.execute") {
258
272
  const { requestId, commandName, command, args } = event.data;
259
273
  void this._executeCommandAndRespond(requestId, commandName, command, args);
@@ -385,6 +399,31 @@ class CopilotSession {
385
399
  }
386
400
  }
387
401
  }
402
+ /**
403
+ * Executes an MCP auth handler and sends the result back via RPC.
404
+ * @internal
405
+ */
406
+ async _executeMcpAuthAndRespond(request) {
407
+ try {
408
+ const result = await this.mcpAuthHandler(request, { sessionId: this.sessionId });
409
+ const response = result && "accessToken" in result ? { kind: "token", ...result } : { kind: "cancelled" };
410
+ await this.rpc.mcp.oauth.handlePendingRequest({
411
+ requestId: request.requestId,
412
+ result: response
413
+ });
414
+ } catch (_error) {
415
+ try {
416
+ await this.rpc.mcp.oauth.handlePendingRequest({
417
+ requestId: request.requestId,
418
+ result: { kind: "cancelled" }
419
+ });
420
+ } catch (rpcError) {
421
+ if (!(rpcError instanceof ConnectionError || rpcError instanceof ResponseError)) {
422
+ throw rpcError;
423
+ }
424
+ }
425
+ }
426
+ }
388
427
  /**
389
428
  * Executes a command handler and sends the result back via RPC.
390
429
  * @internal
@@ -510,7 +549,7 @@ class CopilotSession {
510
549
  };
511
550
  }
512
551
  /**
513
- * Registers per-provider {@link GetBearerToken} callbacks for BYOK providers
552
+ * Registers per-provider {@link BearerTokenProvider} callbacks for BYOK providers
514
553
  * configured with managed-identity / on-demand bearer-token auth.
515
554
  *
516
555
  * The runtime never receives the callback itself; the SDK strips it from the
@@ -540,7 +579,8 @@ class CopilotSession {
540
579
  );
541
580
  }
542
581
  const token = await callback({
543
- providerName: params.providerName
582
+ providerName: params.providerName,
583
+ sessionId: params.sessionId
544
584
  });
545
585
  return { token };
546
586
  }
package/dist/types.d.ts CHANGED
@@ -1280,6 +1280,68 @@ export type ReasoningEffort = "low" | "medium" | "high" | "xhigh";
1280
1280
  * long-context tier when the selected model supports it.
1281
1281
  */
1282
1282
  export type ContextTier = "default" | "long_context";
1283
+ /** Parsed parameters from an MCP server's WWW-Authenticate response. */
1284
+ export interface McpAuthWwwAuthenticateParams {
1285
+ /** Parsed resource_metadata URL used for protected-resource metadata discovery, if present. */
1286
+ resourceMetadataUrl?: string;
1287
+ /** Parsed OAuth scope, if present. */
1288
+ scope?: string;
1289
+ /** Parsed OAuth error, if present. */
1290
+ error?: string;
1291
+ }
1292
+ /** Static OAuth client configuration supplied by the MCP server, if available. */
1293
+ export interface McpAuthStaticClientConfig {
1294
+ /** OAuth client ID for the server. */
1295
+ clientId: string;
1296
+ /** Optional OAuth client secret for confidential static clients. */
1297
+ clientSecret?: string;
1298
+ /** Optional non-default OAuth grant type. */
1299
+ grantType?: "client_credentials";
1300
+ /** Whether this is a public OAuth client. */
1301
+ publicClient?: boolean;
1302
+ }
1303
+ /** MCP OAuth request that the SDK host can satisfy with a host-acquired token. */
1304
+ export interface McpAuthRequest {
1305
+ /** Unique request identifier used by the SDK when responding. */
1306
+ requestId: string;
1307
+ /** Display name of the MCP server that requires OAuth. */
1308
+ serverName: string;
1309
+ /** URL of the MCP server that requires OAuth. */
1310
+ serverUrl: string;
1311
+ /** Why the runtime is requesting host-provided OAuth credentials. */
1312
+ reason: "initial" | "refresh" | "reauth" | "upscope";
1313
+ /** Parsed WWW-Authenticate parameters from the MCP server. */
1314
+ wwwAuthenticateParams?: McpAuthWwwAuthenticateParams;
1315
+ /** Raw RFC 9728 protected-resource metadata JSON fetched by the runtime, if available. */
1316
+ resourceMetadata?: string;
1317
+ /** Static OAuth client configuration, if the server specifies one. */
1318
+ staticClientConfig?: McpAuthStaticClientConfig;
1319
+ }
1320
+ /** Host-provided OAuth token data for a pending MCP OAuth request. */
1321
+ export interface McpAuthToken {
1322
+ /** Access token acquired by the SDK host. */
1323
+ accessToken: string;
1324
+ /** OAuth token type. Defaults to Bearer when omitted. */
1325
+ tokenType?: string;
1326
+ /** Token lifetime in seconds, if known. */
1327
+ expiresIn?: number;
1328
+ }
1329
+ /**
1330
+ * Result returned by an MCP auth request handler.
1331
+ *
1332
+ * Return `null`/`undefined` or `{ kind: "cancelled" }` to cancel the pending
1333
+ * OAuth request. Return `{ kind: "token", ... }` to provide host-acquired
1334
+ * OAuth token data.
1335
+ */
1336
+ export type McpAuthResult = ({
1337
+ kind: "token";
1338
+ } & McpAuthToken) | {
1339
+ kind: "cancelled";
1340
+ };
1341
+ /** Callback invoked when an MCP server requires OAuth and the SDK host opted in. */
1342
+ export type McpAuthHandler = (request: McpAuthRequest, context: {
1343
+ sessionId: string;
1344
+ }) => McpAuthResult | McpAuthToken | null | undefined | Promise<McpAuthResult | McpAuthToken | null | undefined>;
1283
1345
  /**
1284
1346
  * Stable extension identity for session participants that provide canvases.
1285
1347
  */
@@ -1531,6 +1593,12 @@ export interface SessionConfigBase {
1531
1593
  * the consumer to resolve via the pending permission RPC.
1532
1594
  */
1533
1595
  onPermissionRequest?: PermissionHandler;
1596
+ /**
1597
+ * Optional handler for MCP OAuth requests from MCP servers.
1598
+ * When provided, the SDK can satisfy MCP server OAuth requests with
1599
+ * host-provided token data or cancellation.
1600
+ */
1601
+ onMcpAuthRequest?: McpAuthHandler;
1534
1602
  /**
1535
1603
  * Handler for user input requests from the agent.
1536
1604
  * When provided, enables the ask_user tool allowing the agent to ask questions.
@@ -1799,7 +1867,7 @@ export interface ResumeSessionConfig extends SessionConfigBase {
1799
1867
  openCanvases?: OpenCanvasInstance[];
1800
1868
  }
1801
1869
  /**
1802
- * Arguments passed to a {@link GetBearerToken} callback when the runtime needs a
1870
+ * Arguments passed to a {@link BearerTokenProvider} callback when the runtime needs a
1803
1871
  * fresh bearer token for a BYOK provider.
1804
1872
  *
1805
1873
  * @experimental Part of the experimental managed-identity / bearer-token-provider
@@ -1814,7 +1882,14 @@ export interface ProviderTokenArgs {
1814
1882
  * The callback closes over its own token scope/audience; the runtime is
1815
1883
  * provider-agnostic and forwards only the provider name.
1816
1884
  */
1817
- providerName: string;
1885
+ readonly providerName: string;
1886
+ /**
1887
+ * Id of the session that triggered this token request. A client-level shared
1888
+ * callback registered for many sessions can use this to resolve the owning
1889
+ * session (e.g. via the client's session lookup) to scope token acquisition
1890
+ * or caching per session.
1891
+ */
1892
+ readonly sessionId: string;
1818
1893
  }
1819
1894
  /**
1820
1895
  * Per-provider callback that resolves a bearer token on demand, returning the
@@ -1828,7 +1903,7 @@ export interface ProviderTokenArgs {
1828
1903
  * @experimental Part of the experimental managed-identity / bearer-token-provider
1829
1904
  * surface and may change or be removed in future SDK or CLI releases.
1830
1905
  */
1831
- export type GetBearerToken = (args: ProviderTokenArgs) => Promise<string>;
1906
+ export type BearerTokenProvider = (args: ProviderTokenArgs) => Promise<string>;
1832
1907
  /**
1833
1908
  * Configuration for a custom API provider.
1834
1909
  */
@@ -1870,12 +1945,14 @@ export interface ProviderConfig {
1870
1945
  * When set, the SDK keeps this function client-side (it is never serialized)
1871
1946
  * and the runtime calls back into this client to acquire a token before each
1872
1947
  * outbound request. The runtime does no caching of its own, so the callback
1873
- * owns token caching and refresh. Mutually exclusive with {@link apiKey} /
1874
- * {@link bearerToken}.
1948
+ * owns token caching and refresh. When set alongside {@link apiKey} /
1949
+ * {@link bearerToken}, this callback takes precedence: the runtime applies
1950
+ * the token it returns as the `Authorization: Bearer` header for each
1951
+ * request and does not send the static credential.
1875
1952
  *
1876
1953
  * @experimental
1877
1954
  */
1878
- getBearerToken?: GetBearerToken;
1955
+ bearerTokenProvider?: BearerTokenProvider;
1879
1956
  /**
1880
1957
  * Azure-specific options
1881
1958
  */
@@ -1960,12 +2037,14 @@ export interface NamedProviderConfig {
1960
2037
  * When set, the SDK keeps this function client-side (it is never serialized)
1961
2038
  * and the runtime calls back into this client to acquire a token before each
1962
2039
  * outbound request. The runtime does no caching of its own, so the callback
1963
- * owns token caching and refresh. Mutually exclusive with {@link apiKey} /
1964
- * {@link bearerToken}.
2040
+ * owns token caching and refresh. When set alongside {@link apiKey} /
2041
+ * {@link bearerToken}, this callback takes precedence: the runtime applies
2042
+ * the token it returns as the `Authorization: Bearer` header for each
2043
+ * request and does not send the static credential.
1965
2044
  *
1966
2045
  * @experimental
1967
2046
  */
1968
- getBearerToken?: GetBearerToken;
2047
+ bearerTokenProvider?: BearerTokenProvider;
1969
2048
  /**
1970
2049
  * Azure-specific options.
1971
2050
  */
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "type": "git",
5
5
  "url": "https://github.com/github/copilot-sdk.git"
6
6
  },
7
- "version": "1.0.4",
7
+ "version": "1.0.5-preview.1",
8
8
  "description": "TypeScript SDK for programmatic control of GitHub Copilot CLI via JSON-RPC",
9
9
  "main": "./dist/cjs/index.js",
10
10
  "types": "./dist/index.d.ts",
@@ -56,7 +56,7 @@
56
56
  "author": "GitHub",
57
57
  "license": "MIT",
58
58
  "dependencies": {
59
- "@github/copilot": "^1.0.65",
59
+ "@github/copilot": "^1.0.66-2",
60
60
  "vscode-jsonrpc": "^8.2.1",
61
61
  "zod": "^4.3.6"
62
62
  },