pi-mcp-adapter 2.2.1 → 2.3.2
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/CHANGELOG.md +36 -0
- package/README.md +1 -2
- package/commands.ts +48 -20
- package/direct-tools.ts +11 -3
- package/index.ts +96 -38
- package/init.ts +19 -3
- package/mcp-auth-flow.ts +378 -0
- package/mcp-auth.ts +277 -0
- package/mcp-callback-server.ts +251 -0
- package/mcp-oauth-provider.ts +265 -0
- package/package.json +14 -6
- package/proxy-modes.ts +31 -0
- package/server-manager.ts +66 -24
- package/types.ts +27 -1
package/server-manager.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
|
2
2
|
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
3
3
|
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
4
4
|
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
5
|
+
import { UnauthorizedError } from "@modelcontextprotocol/sdk/client/auth.js";
|
|
5
6
|
import type { ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
|
|
6
7
|
import type {
|
|
7
8
|
McpTool,
|
|
@@ -11,9 +12,10 @@ import type {
|
|
|
11
12
|
Transport,
|
|
12
13
|
} from "./types.js";
|
|
13
14
|
import { serverStreamResultPatchNotificationSchema } from "./types.js";
|
|
14
|
-
import { getStoredTokens } from "./oauth-handler.js";
|
|
15
15
|
import { resolveNpxBinary } from "./npx-resolver.js";
|
|
16
16
|
import { logger } from "./logger.js";
|
|
17
|
+
import { McpOAuthProvider } from "./mcp-oauth-provider.js";
|
|
18
|
+
import { supportsOAuth } from "./mcp-auth-flow.js";
|
|
17
19
|
|
|
18
20
|
interface ServerConnection {
|
|
19
21
|
client: Client;
|
|
@@ -23,7 +25,7 @@ interface ServerConnection {
|
|
|
23
25
|
resources: McpResource[];
|
|
24
26
|
lastUsedAt: number;
|
|
25
27
|
inFlight: number;
|
|
26
|
-
status: "connected" | "closed";
|
|
28
|
+
status: "connected" | "closed" | "needs-auth";
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
type UiStreamListener = (serverName: string, notification: ServerStreamResultPatchNotification["params"]) => void;
|
|
@@ -96,7 +98,7 @@ export class McpServerManager {
|
|
|
96
98
|
try {
|
|
97
99
|
await client.connect(transport);
|
|
98
100
|
this.attachAdapterNotificationHandlers(name, client);
|
|
99
|
-
|
|
101
|
+
|
|
100
102
|
// Discover tools and resources
|
|
101
103
|
const [tools, resources] = await Promise.all([
|
|
102
104
|
this.fetchAllTools(client),
|
|
@@ -114,6 +116,24 @@ export class McpServerManager {
|
|
|
114
116
|
status: "connected",
|
|
115
117
|
};
|
|
116
118
|
} catch (error) {
|
|
119
|
+
// Check for UnauthorizedError - server requires OAuth
|
|
120
|
+
if (error instanceof UnauthorizedError && supportsOAuth(definition)) {
|
|
121
|
+
// Clean up both client and transport before reporting needs-auth.
|
|
122
|
+
await client.close().catch(() => {});
|
|
123
|
+
await transport.close().catch(() => {});
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
client,
|
|
127
|
+
transport,
|
|
128
|
+
definition,
|
|
129
|
+
tools: [],
|
|
130
|
+
resources: [],
|
|
131
|
+
lastUsedAt: Date.now(),
|
|
132
|
+
inFlight: 0,
|
|
133
|
+
status: "needs-auth",
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
117
137
|
// Clean up both client and transport on any error
|
|
118
138
|
await client.close().catch(() => {});
|
|
119
139
|
await transport.close().catch(() => {});
|
|
@@ -121,11 +141,16 @@ export class McpServerManager {
|
|
|
121
141
|
}
|
|
122
142
|
}
|
|
123
143
|
|
|
124
|
-
private async createHttpTransport(
|
|
144
|
+
private async createHttpTransport(
|
|
145
|
+
definition: ServerDefinition,
|
|
146
|
+
serverName: string
|
|
147
|
+
): Promise<Transport> {
|
|
125
148
|
const url = new URL(definition.url!);
|
|
149
|
+
|
|
150
|
+
// Build headers first (including any bearer token)
|
|
126
151
|
const headers = resolveHeaders(definition.headers) ?? {};
|
|
127
152
|
|
|
128
|
-
//
|
|
153
|
+
// For bearer auth, add the token to headers BEFORE creating requestInit
|
|
129
154
|
if (definition.auth === "bearer") {
|
|
130
155
|
const token = definition.bearerToken
|
|
131
156
|
?? (definition.bearerTokenEnv ? process.env[definition.bearerTokenEnv] : undefined);
|
|
@@ -134,41 +159,58 @@ export class McpServerManager {
|
|
|
134
159
|
}
|
|
135
160
|
}
|
|
136
161
|
|
|
137
|
-
//
|
|
138
|
-
if (definition.auth === "oauth") {
|
|
139
|
-
if (!serverName) {
|
|
140
|
-
throw new Error("Server name required for OAuth authentication");
|
|
141
|
-
}
|
|
142
|
-
const tokens = getStoredTokens(serverName);
|
|
143
|
-
if (!tokens) {
|
|
144
|
-
throw new Error(
|
|
145
|
-
`No OAuth tokens found for "${serverName}". Run /mcp-auth ${serverName} to authenticate.`
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
headers["Authorization"] = `Bearer ${tokens.access_token}`;
|
|
149
|
-
}
|
|
150
|
-
|
|
162
|
+
// Create request init with headers (Authorization now included for bearer auth)
|
|
151
163
|
const requestInit = Object.keys(headers).length > 0 ? { headers } : undefined;
|
|
152
164
|
|
|
165
|
+
// For OAuth servers, create an auth provider
|
|
166
|
+
let authProvider: McpOAuthProvider | undefined;
|
|
167
|
+
if (supportsOAuth(definition)) {
|
|
168
|
+
// Extract OAuth config (handles both object and false cases)
|
|
169
|
+
const oauthConfig = definition.oauth === false ? {} : {
|
|
170
|
+
grantType: definition.oauth?.grantType,
|
|
171
|
+
clientId: definition.oauth?.clientId,
|
|
172
|
+
clientSecret: definition.oauth?.clientSecret,
|
|
173
|
+
scope: definition.oauth?.scope,
|
|
174
|
+
};
|
|
175
|
+
authProvider = new McpOAuthProvider(
|
|
176
|
+
serverName,
|
|
177
|
+
definition.url!,
|
|
178
|
+
oauthConfig,
|
|
179
|
+
{
|
|
180
|
+
onRedirect: async (_authUrl) => {
|
|
181
|
+
// URL is captured by startAuth, no need to log
|
|
182
|
+
},
|
|
183
|
+
}
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
153
187
|
// Try StreamableHTTP first (modern MCP servers)
|
|
154
|
-
const streamableTransport = new StreamableHTTPClientTransport(url, {
|
|
188
|
+
const streamableTransport = new StreamableHTTPClientTransport(url, {
|
|
189
|
+
requestInit,
|
|
190
|
+
authProvider,
|
|
191
|
+
});
|
|
155
192
|
|
|
156
193
|
try {
|
|
157
194
|
// Create a test client to verify the transport works
|
|
158
|
-
const testClient = new Client({ name: "pi-mcp-probe", version: "1.
|
|
195
|
+
const testClient = new Client({ name: "pi-mcp-probe", version: "2.1.2" });
|
|
159
196
|
await testClient.connect(streamableTransport);
|
|
160
197
|
await testClient.close().catch(() => {});
|
|
161
198
|
// Close probe transport before creating fresh one
|
|
162
199
|
await streamableTransport.close().catch(() => {});
|
|
163
200
|
|
|
164
201
|
// StreamableHTTP works - create fresh transport for actual use
|
|
165
|
-
return new StreamableHTTPClientTransport(url, { requestInit });
|
|
166
|
-
} catch {
|
|
202
|
+
return new StreamableHTTPClientTransport(url, { requestInit, authProvider });
|
|
203
|
+
} catch (error) {
|
|
167
204
|
// StreamableHTTP failed, close and try SSE fallback
|
|
168
205
|
await streamableTransport.close().catch(() => {});
|
|
169
206
|
|
|
207
|
+
// If this was an UnauthorizedError, don't try SSE - the server needs auth
|
|
208
|
+
if (error instanceof UnauthorizedError) {
|
|
209
|
+
throw error;
|
|
210
|
+
}
|
|
211
|
+
|
|
170
212
|
// SSE is the legacy transport
|
|
171
|
-
return new SSEClientTransport(url, { requestInit });
|
|
213
|
+
return new SSEClientTransport(url, { requestInit, authProvider });
|
|
172
214
|
}
|
|
173
215
|
}
|
|
174
216
|
|
package/types.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdi
|
|
|
3
3
|
import type { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
4
4
|
import type { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
5
5
|
import type { TextContent, ImageContent } from "@mariozechner/pi-ai";
|
|
6
|
+
import type { UiStreamMode } from "./ui-stream-types.js";
|
|
6
7
|
|
|
7
8
|
// Transport type (stdio + HTTP)
|
|
8
9
|
export type Transport =
|
|
@@ -261,6 +262,18 @@ export interface McpContent {
|
|
|
261
262
|
// Pi content block type
|
|
262
263
|
export type ContentBlock = TextContent | ImageContent;
|
|
263
264
|
|
|
265
|
+
// OAuth configuration (SDK handles auto-discovery and dynamic registration)
|
|
266
|
+
export interface OAuthConfig {
|
|
267
|
+
/** OAuth grant type (defaults to authorization_code) */
|
|
268
|
+
grantType?: "authorization_code" | "client_credentials";
|
|
269
|
+
/** Pre-registered client ID (optional, dynamic registration used if not provided) */
|
|
270
|
+
clientId?: string;
|
|
271
|
+
/** Client secret for confidential clients */
|
|
272
|
+
clientSecret?: string;
|
|
273
|
+
/** Requested OAuth scopes */
|
|
274
|
+
scope?: string;
|
|
275
|
+
}
|
|
276
|
+
|
|
264
277
|
// Server configuration
|
|
265
278
|
export interface ServerEntry {
|
|
266
279
|
command?: string;
|
|
@@ -270,9 +283,22 @@ export interface ServerEntry {
|
|
|
270
283
|
// HTTP fields
|
|
271
284
|
url?: string;
|
|
272
285
|
headers?: Record<string, string>;
|
|
273
|
-
|
|
286
|
+
/**
|
|
287
|
+
* Authentication type:
|
|
288
|
+
* - 'oauth' - Use OAuth 2.1 (auto-discovers endpoints, supports dynamic client registration)
|
|
289
|
+
* - 'bearer' - Use static Bearer token
|
|
290
|
+
* - false - Disable authentication
|
|
291
|
+
* If not specified and url is present, OAuth will be auto-detected
|
|
292
|
+
*/
|
|
293
|
+
auth?: "oauth" | "bearer" | false;
|
|
274
294
|
bearerToken?: string;
|
|
275
295
|
bearerTokenEnv?: string;
|
|
296
|
+
/**
|
|
297
|
+
* OAuth configuration (optional).
|
|
298
|
+
* If not provided, the SDK will attempt dynamic client registration.
|
|
299
|
+
* Set to false to explicitly disable OAuth for this server.
|
|
300
|
+
*/
|
|
301
|
+
oauth?: OAuthConfig | false;
|
|
276
302
|
lifecycle?: "keep-alive" | "lazy" | "eager";
|
|
277
303
|
idleTimeout?: number; // minutes, overrides global setting
|
|
278
304
|
// Resource handling
|