mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-19 09:33:53 +00:00
feat(cli) - Define shared interface for storage (#7049)
Co-authored-by: Shi Shu <shii@google.com>
This commit is contained in:
@@ -90,9 +90,9 @@ export * from './tools/mcp-tool.js';
|
|||||||
// MCP OAuth
|
// MCP OAuth
|
||||||
export { MCPOAuthProvider } from './mcp/oauth-provider.js';
|
export { MCPOAuthProvider } from './mcp/oauth-provider.js';
|
||||||
export type {
|
export type {
|
||||||
MCPOAuthToken,
|
OAuthToken,
|
||||||
MCPOAuthCredentials,
|
OAuthCredentials,
|
||||||
} from './mcp/oauth-token-storage.js';
|
} from './mcp/token-storage/types.js';
|
||||||
export { MCPOAuthTokenStorage } from './mcp/oauth-token-storage.js';
|
export { MCPOAuthTokenStorage } from './mcp/oauth-token-storage.js';
|
||||||
export type { MCPOAuthConfig } from './mcp/oauth-provider.js';
|
export type { MCPOAuthConfig } from './mcp/oauth-provider.js';
|
||||||
export type {
|
export type {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import type {
|
|||||||
OAuthClientRegistrationResponse,
|
OAuthClientRegistrationResponse,
|
||||||
} from './oauth-provider.js';
|
} from './oauth-provider.js';
|
||||||
import { MCPOAuthProvider } from './oauth-provider.js';
|
import { MCPOAuthProvider } from './oauth-provider.js';
|
||||||
import type { MCPOAuthToken } from './oauth-token-storage.js';
|
import type { OAuthToken } from './token-storage/types.js';
|
||||||
import { MCPOAuthTokenStorage } from './oauth-token-storage.js';
|
import { MCPOAuthTokenStorage } from './oauth-token-storage.js';
|
||||||
|
|
||||||
// Mock fetch globally
|
// Mock fetch globally
|
||||||
@@ -101,7 +101,7 @@ describe('MCPOAuthProvider', () => {
|
|||||||
audiences: ['https://api.example.com'],
|
audiences: ['https://api.example.com'],
|
||||||
};
|
};
|
||||||
|
|
||||||
const mockToken: MCPOAuthToken = {
|
const mockToken: OAuthToken = {
|
||||||
accessToken: 'access_token_123',
|
accessToken: 'access_token_123',
|
||||||
refreshToken: 'refresh_token_456',
|
refreshToken: 'refresh_token_456',
|
||||||
tokenType: 'Bearer',
|
tokenType: 'Bearer',
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import * as http from 'node:http';
|
|||||||
import * as crypto from 'node:crypto';
|
import * as crypto from 'node:crypto';
|
||||||
import { URL } from 'node:url';
|
import { URL } from 'node:url';
|
||||||
import { openBrowserSecurely } from '../utils/secure-browser-launcher.js';
|
import { openBrowserSecurely } from '../utils/secure-browser-launcher.js';
|
||||||
import type { MCPOAuthToken } from './oauth-token-storage.js';
|
import type { OAuthToken } from './token-storage/types.js';
|
||||||
import { MCPOAuthTokenStorage } from './oauth-token-storage.js';
|
import { MCPOAuthTokenStorage } from './oauth-token-storage.js';
|
||||||
import { getErrorMessage } from '../utils/errors.js';
|
import { getErrorMessage } from '../utils/errors.js';
|
||||||
import { OAuthUtils } from './oauth-utils.js';
|
import { OAuthUtils } from './oauth-utils.js';
|
||||||
@@ -583,7 +583,7 @@ export class MCPOAuthProvider {
|
|||||||
serverName: string,
|
serverName: string,
|
||||||
config: MCPOAuthConfig,
|
config: MCPOAuthConfig,
|
||||||
mcpServerUrl?: string,
|
mcpServerUrl?: string,
|
||||||
): Promise<MCPOAuthToken> {
|
): Promise<OAuthToken> {
|
||||||
// If no authorization URL is provided, try to discover OAuth configuration
|
// If no authorization URL is provided, try to discover OAuth configuration
|
||||||
if (!config.authorizationUrl && mcpServerUrl) {
|
if (!config.authorizationUrl && mcpServerUrl) {
|
||||||
console.log(
|
console.log(
|
||||||
@@ -777,7 +777,7 @@ export class MCPOAuthProvider {
|
|||||||
throw new Error('No access token received from token endpoint');
|
throw new Error('No access token received from token endpoint');
|
||||||
}
|
}
|
||||||
|
|
||||||
const token: MCPOAuthToken = {
|
const token: OAuthToken = {
|
||||||
accessToken: tokenResponse.access_token,
|
accessToken: tokenResponse.access_token,
|
||||||
tokenType: tokenResponse.token_type || 'Bearer',
|
tokenType: tokenResponse.token_type || 'Bearer',
|
||||||
refreshToken: tokenResponse.refresh_token,
|
refreshToken: tokenResponse.refresh_token,
|
||||||
@@ -863,7 +863,7 @@ export class MCPOAuthProvider {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Update stored token
|
// Update stored token
|
||||||
const newToken: MCPOAuthToken = {
|
const newToken: OAuthToken = {
|
||||||
accessToken: newTokenResponse.access_token,
|
accessToken: newTokenResponse.access_token,
|
||||||
tokenType: newTokenResponse.token_type,
|
tokenType: newTokenResponse.token_type,
|
||||||
refreshToken: newTokenResponse.refresh_token || token.refreshToken,
|
refreshToken: newTokenResponse.refresh_token || token.refreshToken,
|
||||||
|
|||||||
@@ -7,11 +7,8 @@
|
|||||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||||
import { promises as fs } from 'node:fs';
|
import { promises as fs } from 'node:fs';
|
||||||
import * as path from 'node:path';
|
import * as path from 'node:path';
|
||||||
import type {
|
|
||||||
MCPOAuthToken,
|
|
||||||
MCPOAuthCredentials,
|
|
||||||
} from './oauth-token-storage.js';
|
|
||||||
import { MCPOAuthTokenStorage } from './oauth-token-storage.js';
|
import { MCPOAuthTokenStorage } from './oauth-token-storage.js';
|
||||||
|
import type { OAuthToken, OAuthCredentials } from './token-storage/types.js';
|
||||||
|
|
||||||
// Mock file system operations
|
// Mock file system operations
|
||||||
vi.mock('node:fs', () => ({
|
vi.mock('node:fs', () => ({
|
||||||
@@ -29,7 +26,7 @@ vi.mock('node:os', () => ({
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
describe('MCPOAuthTokenStorage', () => {
|
describe('MCPOAuthTokenStorage', () => {
|
||||||
const mockToken: MCPOAuthToken = {
|
const mockToken: OAuthToken = {
|
||||||
accessToken: 'access_token_123',
|
accessToken: 'access_token_123',
|
||||||
refreshToken: 'refresh_token_456',
|
refreshToken: 'refresh_token_456',
|
||||||
tokenType: 'Bearer',
|
tokenType: 'Bearer',
|
||||||
@@ -37,7 +34,7 @@ describe('MCPOAuthTokenStorage', () => {
|
|||||||
expiresAt: Date.now() + 3600000, // 1 hour from now
|
expiresAt: Date.now() + 3600000, // 1 hour from now
|
||||||
};
|
};
|
||||||
|
|
||||||
const mockCredentials: MCPOAuthCredentials = {
|
const mockCredentials: OAuthCredentials = {
|
||||||
serverName: 'test-server',
|
serverName: 'test-server',
|
||||||
token: mockToken,
|
token: mockToken,
|
||||||
clientId: 'test-client-id',
|
clientId: 'test-client-id',
|
||||||
|
|||||||
@@ -8,29 +8,7 @@ import { promises as fs } from 'node:fs';
|
|||||||
import * as path from 'node:path';
|
import * as path from 'node:path';
|
||||||
import { Storage } from '../config/storage.js';
|
import { Storage } from '../config/storage.js';
|
||||||
import { getErrorMessage } from '../utils/errors.js';
|
import { getErrorMessage } from '../utils/errors.js';
|
||||||
|
import type { OAuthToken, OAuthCredentials } from './token-storage/types.js';
|
||||||
/**
|
|
||||||
* Interface for MCP OAuth tokens.
|
|
||||||
*/
|
|
||||||
export interface MCPOAuthToken {
|
|
||||||
accessToken: string;
|
|
||||||
refreshToken?: string;
|
|
||||||
expiresAt?: number;
|
|
||||||
tokenType: string;
|
|
||||||
scope?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface for stored MCP OAuth credentials.
|
|
||||||
*/
|
|
||||||
export interface MCPOAuthCredentials {
|
|
||||||
serverName: string;
|
|
||||||
token: MCPOAuthToken;
|
|
||||||
clientId?: string;
|
|
||||||
tokenUrl?: string;
|
|
||||||
mcpServerUrl?: string;
|
|
||||||
updatedAt: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class for managing MCP OAuth token storage and retrieval.
|
* Class for managing MCP OAuth token storage and retrieval.
|
||||||
@@ -58,13 +36,13 @@ export class MCPOAuthTokenStorage {
|
|||||||
*
|
*
|
||||||
* @returns A map of server names to credentials
|
* @returns A map of server names to credentials
|
||||||
*/
|
*/
|
||||||
static async loadTokens(): Promise<Map<string, MCPOAuthCredentials>> {
|
static async loadTokens(): Promise<Map<string, OAuthCredentials>> {
|
||||||
const tokenMap = new Map<string, MCPOAuthCredentials>();
|
const tokenMap = new Map<string, OAuthCredentials>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const tokenFile = this.getTokenFilePath();
|
const tokenFile = this.getTokenFilePath();
|
||||||
const data = await fs.readFile(tokenFile, 'utf-8');
|
const data = await fs.readFile(tokenFile, 'utf-8');
|
||||||
const tokens = JSON.parse(data) as MCPOAuthCredentials[];
|
const tokens = JSON.parse(data) as OAuthCredentials[];
|
||||||
|
|
||||||
for (const credential of tokens) {
|
for (const credential of tokens) {
|
||||||
tokenMap.set(credential.serverName, credential);
|
tokenMap.set(credential.serverName, credential);
|
||||||
@@ -92,7 +70,7 @@ export class MCPOAuthTokenStorage {
|
|||||||
*/
|
*/
|
||||||
static async saveToken(
|
static async saveToken(
|
||||||
serverName: string,
|
serverName: string,
|
||||||
token: MCPOAuthToken,
|
token: OAuthToken,
|
||||||
clientId?: string,
|
clientId?: string,
|
||||||
tokenUrl?: string,
|
tokenUrl?: string,
|
||||||
mcpServerUrl?: string,
|
mcpServerUrl?: string,
|
||||||
@@ -101,7 +79,7 @@ export class MCPOAuthTokenStorage {
|
|||||||
|
|
||||||
const tokens = await this.loadTokens();
|
const tokens = await this.loadTokens();
|
||||||
|
|
||||||
const credential: MCPOAuthCredentials = {
|
const credential: OAuthCredentials = {
|
||||||
serverName,
|
serverName,
|
||||||
token,
|
token,
|
||||||
clientId,
|
clientId,
|
||||||
@@ -135,9 +113,7 @@ export class MCPOAuthTokenStorage {
|
|||||||
* @param serverName The name of the MCP server
|
* @param serverName The name of the MCP server
|
||||||
* @returns The stored credentials or null if not found
|
* @returns The stored credentials or null if not found
|
||||||
*/
|
*/
|
||||||
static async getToken(
|
static async getToken(serverName: string): Promise<OAuthCredentials | null> {
|
||||||
serverName: string,
|
|
||||||
): Promise<MCPOAuthCredentials | null> {
|
|
||||||
const tokens = await this.loadTokens();
|
const tokens = await this.loadTokens();
|
||||||
return tokens.get(serverName) || null;
|
return tokens.get(serverName) || null;
|
||||||
}
|
}
|
||||||
@@ -177,7 +153,7 @@ export class MCPOAuthTokenStorage {
|
|||||||
* @param token The token to check
|
* @param token The token to check
|
||||||
* @returns True if the token is expired
|
* @returns True if the token is expired
|
||||||
*/
|
*/
|
||||||
static isTokenExpired(token: MCPOAuthToken): boolean {
|
static isTokenExpired(token: OAuthToken): boolean {
|
||||||
if (!token.expiresAt) {
|
if (!token.expiresAt) {
|
||||||
return false; // No expiry, assume valid
|
return false; // No expiry, assume valid
|
||||||
}
|
}
|
||||||
|
|||||||
37
packages/core/src/mcp/token-storage/types.ts
Normal file
37
packages/core/src/mcp/token-storage/types.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Copyright 2025 Google LLC
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface for OAuth tokens.
|
||||||
|
*/
|
||||||
|
export interface OAuthToken {
|
||||||
|
accessToken: string;
|
||||||
|
refreshToken?: string;
|
||||||
|
expiresAt?: number;
|
||||||
|
tokenType: string;
|
||||||
|
scope?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface for stored OAuth credentials.
|
||||||
|
*/
|
||||||
|
export interface OAuthCredentials {
|
||||||
|
serverName: string;
|
||||||
|
token: OAuthToken;
|
||||||
|
clientId?: string;
|
||||||
|
tokenUrl?: string;
|
||||||
|
mcpServerUrl?: string;
|
||||||
|
updatedAt: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TokenStorage {
|
||||||
|
getCredentials(serverName: string): Promise<OAuthCredentials | null>;
|
||||||
|
setCredentials(credentials: OAuthCredentials): Promise<void>;
|
||||||
|
deleteCredentials(serverName: string): Promise<void>;
|
||||||
|
listServers(): Promise<string[]>;
|
||||||
|
getAllCredentials(): Promise<Map<string, OAuthCredentials>>;
|
||||||
|
clearAll(): Promise<void>;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user