@crush-protocol/mcp-client 0.4.3 → 0.4.4
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/INSTRUCTIONS.md +16 -0
- package/README.md +269 -403
- package/dist/cli.js +6 -0
- package/dist/mcp/authPreflight.d.ts +9 -0
- package/dist/mcp/authPreflight.js +26 -0
- package/dist/mcp/oauthProvider.d.ts +2 -0
- package/dist/mcp/oauthProvider.js +8 -1
- package/dist/mcp/oauthRemoteClient.d.ts +3 -0
- package/dist/mcp/oauthRemoteClient.js +31 -18
- package/dist/mcp/proxy.js +29 -114
- package/dist/onboarding/cliOutput.d.ts +2 -0
- package/dist/onboarding/cliOutput.js +15 -0
- package/package.json +24 -3
- package/dist/__tests__/cliOutput.test.d.ts +0 -1
- package/dist/__tests__/cliOutput.test.js +0 -34
- package/dist/__tests__/e2e.test.d.ts +0 -1
- package/dist/__tests__/e2e.test.js +0 -50
- package/dist/__tests__/oauthProvider.test.d.ts +0 -1
- package/dist/__tests__/oauthProvider.test.js +0 -45
- package/dist/__tests__/oauthStorage.test.d.ts +0 -1
- package/dist/__tests__/oauthStorage.test.js +0 -52
package/dist/cli.js
CHANGED
|
@@ -3,6 +3,7 @@ import "dotenv/config";
|
|
|
3
3
|
import { BacktestClient } from "./backtest/backtestClient.js";
|
|
4
4
|
import { ClickHouseDirectClient } from "./clickhouse/directClient.js";
|
|
5
5
|
import { DEFAULT_MCP_SERVER_URL } from "./config.js";
|
|
6
|
+
import { assertManualLoginNotRequired } from "./mcp/authPreflight.js";
|
|
6
7
|
import { OAuthRemoteMcpClient } from "./mcp/oauthRemoteClient.js";
|
|
7
8
|
import { getCachedAuthStatus, loadStoredTokens } from "./mcp/oauthStorage.js";
|
|
8
9
|
import { runProxy } from "./mcp/proxy.js";
|
|
@@ -73,6 +74,11 @@ const createSmartClient = (flags) => {
|
|
|
73
74
|
};
|
|
74
75
|
};
|
|
75
76
|
const connectClient = async (flags) => {
|
|
77
|
+
const explicitToken = getExplicitToken(flags);
|
|
78
|
+
if (!explicitToken) {
|
|
79
|
+
const serverUrl = getServerUrl(typeof flags.server === "string" ? flags.server : undefined);
|
|
80
|
+
await assertManualLoginNotRequired(serverUrl);
|
|
81
|
+
}
|
|
76
82
|
const { client, needsOAuthConnect } = createSmartClient(flags);
|
|
77
83
|
if (needsOAuthConnect) {
|
|
78
84
|
await client.connect();
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type CachedAuthStatus } from "./oauthStorage.js";
|
|
2
|
+
export type AuthPreflightResult = {
|
|
3
|
+
authStatus: CachedAuthStatus;
|
|
4
|
+
requiresManualLogin: boolean;
|
|
5
|
+
canAutoRefreshLogin: boolean;
|
|
6
|
+
};
|
|
7
|
+
export declare const getAuthPreflight: (serverUrl: string) => Promise<AuthPreflightResult>;
|
|
8
|
+
export declare const assertManualLoginNotRequired: (serverUrl: string) => Promise<AuthPreflightResult>;
|
|
9
|
+
export declare const getPreflightStatusMessage: (preflight: AuthPreflightResult) => string | null;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { formatAutomaticRefreshMessage, formatLoginRequiredMessage } from "../onboarding/cliOutput.js";
|
|
2
|
+
import { getCachedAuthStatus } from "./oauthStorage.js";
|
|
3
|
+
export const getAuthPreflight = async (serverUrl) => {
|
|
4
|
+
const authStatus = await getCachedAuthStatus(serverUrl);
|
|
5
|
+
return {
|
|
6
|
+
authStatus,
|
|
7
|
+
requiresManualLogin: authStatus.status === "not_authenticated",
|
|
8
|
+
canAutoRefreshLogin: authStatus.status !== "not_authenticated",
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export const assertManualLoginNotRequired = async (serverUrl) => {
|
|
12
|
+
const preflight = await getAuthPreflight(serverUrl);
|
|
13
|
+
if (preflight.requiresManualLogin) {
|
|
14
|
+
throw new Error(formatLoginRequiredMessage(serverUrl));
|
|
15
|
+
}
|
|
16
|
+
return preflight;
|
|
17
|
+
};
|
|
18
|
+
export const getPreflightStatusMessage = (preflight) => {
|
|
19
|
+
if (preflight.requiresManualLogin) {
|
|
20
|
+
return formatLoginRequiredMessage(preflight.authStatus.serverUrl);
|
|
21
|
+
}
|
|
22
|
+
if (preflight.authStatus.status === "registered") {
|
|
23
|
+
return formatAutomaticRefreshMessage(preflight.authStatus.serverUrl);
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
};
|
|
@@ -9,6 +9,7 @@ export type InteractiveOAuthProviderOptions = {
|
|
|
9
9
|
redirectPath?: string;
|
|
10
10
|
storageDir?: string;
|
|
11
11
|
openBrowser?: boolean;
|
|
12
|
+
authorizationOutput?: "stdout" | "stderr" | "silent";
|
|
12
13
|
onAuthorizationUrl?: (authorizationUrl: URL) => void | Promise<void>;
|
|
13
14
|
};
|
|
14
15
|
export declare class InteractiveOAuthProvider implements OAuthClientProvider {
|
|
@@ -17,6 +18,7 @@ export declare class InteractiveOAuthProvider implements OAuthClientProvider {
|
|
|
17
18
|
private readonly storageFile;
|
|
18
19
|
private readonly scope;
|
|
19
20
|
private readonly openBrowserByDefault;
|
|
21
|
+
private readonly authorizationOutput;
|
|
20
22
|
private readonly onAuthorizationUrl?;
|
|
21
23
|
private callbackServer?;
|
|
22
24
|
private pendingAuthorization?;
|
|
@@ -72,6 +72,7 @@ export class InteractiveOAuthProvider {
|
|
|
72
72
|
storageFile;
|
|
73
73
|
scope;
|
|
74
74
|
openBrowserByDefault;
|
|
75
|
+
authorizationOutput;
|
|
75
76
|
onAuthorizationUrl;
|
|
76
77
|
callbackServer;
|
|
77
78
|
pendingAuthorization;
|
|
@@ -82,6 +83,7 @@ export class InteractiveOAuthProvider {
|
|
|
82
83
|
this.redirectUrl = new URL(`http://127.0.0.1:${redirectPort}${redirectPath}`);
|
|
83
84
|
this.scope = options.scope ?? DEFAULT_OAUTH_SCOPE;
|
|
84
85
|
this.openBrowserByDefault = options.openBrowser ?? true;
|
|
86
|
+
this.authorizationOutput = options.authorizationOutput ?? "stdout";
|
|
85
87
|
const storageDir = options.storageDir ?? defaultStorageDir();
|
|
86
88
|
this.storageFile = getStorageFileForServer(options.serverUrl, storageDir);
|
|
87
89
|
this.onAuthorizationUrl = options.onAuthorizationUrl;
|
|
@@ -132,7 +134,12 @@ export class InteractiveOAuthProvider {
|
|
|
132
134
|
// Fall back to printing the URL when no browser launcher is available.
|
|
133
135
|
}
|
|
134
136
|
}
|
|
135
|
-
|
|
137
|
+
if (this.authorizationOutput === "stdout") {
|
|
138
|
+
process.stdout.write(`\nOpen this URL to authorize Crush MCP:\n${authorizationUrl.toString()}\n\n`);
|
|
139
|
+
}
|
|
140
|
+
else if (this.authorizationOutput === "stderr") {
|
|
141
|
+
process.stderr.write(`\nOpen this URL to authorize Crush MCP:\n${authorizationUrl.toString()}\n\n`);
|
|
142
|
+
}
|
|
136
143
|
}
|
|
137
144
|
async saveCodeVerifier(codeVerifier) {
|
|
138
145
|
const data = await this.loadState();
|
|
@@ -16,6 +16,9 @@ export declare class OAuthRemoteMcpClient implements McpClientLike {
|
|
|
16
16
|
private readonly transport;
|
|
17
17
|
private readonly authProvider;
|
|
18
18
|
constructor(options: OAuthRemoteMcpClientOptions);
|
|
19
|
+
private isAuthError;
|
|
20
|
+
private recoverAuthorization;
|
|
21
|
+
private withReauthorization;
|
|
19
22
|
connect(): Promise<void>;
|
|
20
23
|
ensureAuthorized(): Promise<void>;
|
|
21
24
|
runAuthFlow(): Promise<AuthResult>;
|
|
@@ -35,25 +35,38 @@ export class OAuthRemoteMcpClient {
|
|
|
35
35
|
fetch: options.fetch,
|
|
36
36
|
});
|
|
37
37
|
}
|
|
38
|
+
isAuthError(error) {
|
|
39
|
+
return (error instanceof UnauthorizedError || String(error).includes("Unauthorized") || String(error).includes("401"));
|
|
40
|
+
}
|
|
41
|
+
async recoverAuthorization(error) {
|
|
42
|
+
if (!this.isAuthError(error)) {
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
const authResult = await auth(this.authProvider, {
|
|
46
|
+
serverUrl: this.options.serverUrl,
|
|
47
|
+
scope: this.options.scope,
|
|
48
|
+
fetchFn: this.options.fetch,
|
|
49
|
+
});
|
|
50
|
+
if (authResult === "REDIRECT") {
|
|
51
|
+
const authorizationCode = await this.authProvider.waitForAuthorizationCode();
|
|
52
|
+
await this.transport.finishAuth(authorizationCode);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async withReauthorization(operation) {
|
|
56
|
+
try {
|
|
57
|
+
return await operation();
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
await this.recoverAuthorization(error);
|
|
61
|
+
return operation();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
38
64
|
async connect() {
|
|
39
65
|
try {
|
|
40
66
|
await this.client.connect(this.transport);
|
|
41
67
|
}
|
|
42
68
|
catch (error) {
|
|
43
|
-
|
|
44
|
-
throw error;
|
|
45
|
-
}
|
|
46
|
-
const authResult = await auth(this.authProvider, {
|
|
47
|
-
serverUrl: this.options.serverUrl,
|
|
48
|
-
scope: this.options.scope,
|
|
49
|
-
fetchFn: this.options.fetch,
|
|
50
|
-
});
|
|
51
|
-
if (authResult !== "REDIRECT") {
|
|
52
|
-
await this.client.connect(this.transport);
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
const authorizationCode = await this.authProvider.waitForAuthorizationCode();
|
|
56
|
-
await this.transport.finishAuth(authorizationCode);
|
|
69
|
+
await this.recoverAuthorization(error);
|
|
57
70
|
await this.client.connect(this.transport);
|
|
58
71
|
}
|
|
59
72
|
}
|
|
@@ -82,15 +95,15 @@ export class OAuthRemoteMcpClient {
|
|
|
82
95
|
await this.transport.terminateSession();
|
|
83
96
|
}
|
|
84
97
|
async ping() {
|
|
85
|
-
return this.client.ping();
|
|
98
|
+
return this.withReauthorization(() => this.client.ping());
|
|
86
99
|
}
|
|
87
100
|
async listTools() {
|
|
88
|
-
return this.client.listTools();
|
|
101
|
+
return this.withReauthorization(() => this.client.listTools());
|
|
89
102
|
}
|
|
90
103
|
async callTool(name, args = {}) {
|
|
91
|
-
return this.client.callTool({
|
|
104
|
+
return this.withReauthorization(() => this.client.callTool({
|
|
92
105
|
name,
|
|
93
106
|
arguments: args,
|
|
94
|
-
});
|
|
107
|
+
}));
|
|
95
108
|
}
|
|
96
109
|
}
|
package/dist/mcp/proxy.js
CHANGED
|
@@ -3,159 +3,74 @@
|
|
|
3
3
|
*
|
|
4
4
|
* 原理:
|
|
5
5
|
* AI 工具(Cursor/Claude/Antigravity)通过 stdio 连接本 proxy,
|
|
6
|
-
* proxy
|
|
7
|
-
* 将请求转发到远程 MCP Server(Streamable HTTP)。
|
|
6
|
+
* proxy 将请求转发到远程 MCP Server(Streamable HTTP)。
|
|
8
7
|
*
|
|
9
8
|
* 使用:
|
|
10
9
|
* npx @crush-protocol/mcp-client proxy [SERVER_URL]
|
|
11
10
|
*
|
|
12
11
|
* 用户只需执行一次 `login` 获取 token,所有 AI 工具共享同一份凭证。
|
|
13
12
|
*/
|
|
14
|
-
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
15
|
-
import path from "node:path";
|
|
16
|
-
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
17
|
-
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
18
13
|
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
19
14
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
20
15
|
import { CallToolRequestSchema, ListToolsRequestSchema, PingRequestSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
16
|
+
import { getPreflightStatusMessage } from "./authPreflight.js";
|
|
17
|
+
import { OAuthRemoteMcpClient } from "./oauthRemoteClient.js";
|
|
18
|
+
import { getCachedAuthStatus } from "./oauthStorage.js";
|
|
23
19
|
import { CLIENT_NAME, CLIENT_VERSION } from "./version.js";
|
|
24
|
-
const saveTokens = async (serverUrl, tokens) => {
|
|
25
|
-
const storageFile = getStorageFileForServer(serverUrl, defaultStorageDir());
|
|
26
|
-
let state = {};
|
|
27
|
-
try {
|
|
28
|
-
const raw = await readFile(storageFile, "utf8");
|
|
29
|
-
state = JSON.parse(raw);
|
|
30
|
-
}
|
|
31
|
-
catch {
|
|
32
|
-
// ignore
|
|
33
|
-
}
|
|
34
|
-
state.tokens = tokens;
|
|
35
|
-
await mkdir(path.dirname(storageFile), { recursive: true });
|
|
36
|
-
await writeFile(storageFile, JSON.stringify(state, null, 2), { encoding: "utf8", mode: 0o600 });
|
|
37
|
-
};
|
|
38
|
-
/**
|
|
39
|
-
* 尝试用 refresh_token 刷新 access_token
|
|
40
|
-
*/
|
|
41
|
-
const refreshAccessToken = async (serverUrl, tokens) => {
|
|
42
|
-
if (!tokens.refresh_token)
|
|
43
|
-
return null;
|
|
44
|
-
try {
|
|
45
|
-
// 先获取 token_endpoint
|
|
46
|
-
const metadataUrl = new URL("/.well-known/oauth-authorization-server", serverUrl);
|
|
47
|
-
const metaRes = await fetch(metadataUrl.toString());
|
|
48
|
-
if (!metaRes.ok)
|
|
49
|
-
return null;
|
|
50
|
-
const meta = (await metaRes.json());
|
|
51
|
-
// 获取 client_id
|
|
52
|
-
const storageFile = getStorageFileForServer(serverUrl, defaultStorageDir());
|
|
53
|
-
const raw = await readFile(storageFile, "utf8");
|
|
54
|
-
const state = JSON.parse(raw);
|
|
55
|
-
const clientInfo = state.clientInformation;
|
|
56
|
-
if (!clientInfo?.client_id)
|
|
57
|
-
return null;
|
|
58
|
-
const body = new URLSearchParams({
|
|
59
|
-
grant_type: "refresh_token",
|
|
60
|
-
refresh_token: tokens.refresh_token,
|
|
61
|
-
client_id: clientInfo.client_id,
|
|
62
|
-
});
|
|
63
|
-
const res = await fetch(meta.token_endpoint, {
|
|
64
|
-
method: "POST",
|
|
65
|
-
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
66
|
-
body: body.toString(),
|
|
67
|
-
});
|
|
68
|
-
if (!res.ok)
|
|
69
|
-
return null;
|
|
70
|
-
const newTokens = (await res.json());
|
|
71
|
-
await saveTokens(serverUrl, newTokens);
|
|
72
|
-
return newTokens;
|
|
73
|
-
}
|
|
74
|
-
catch {
|
|
75
|
-
return null;
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
20
|
export async function runProxy(serverUrl) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
21
|
+
const authStatus = await getCachedAuthStatus(serverUrl);
|
|
22
|
+
if (authStatus.status === "not_authenticated") {
|
|
23
|
+
const message = getPreflightStatusMessage({
|
|
24
|
+
authStatus,
|
|
25
|
+
requiresManualLogin: true,
|
|
26
|
+
canAutoRefreshLogin: false,
|
|
27
|
+
});
|
|
28
|
+
process.stderr.write(`${message ?? "Crush needs sign-in on this machine."}\n`);
|
|
86
29
|
process.exit(1);
|
|
87
30
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
Authorization: `Bearer ${token}`,
|
|
93
|
-
},
|
|
94
|
-
},
|
|
31
|
+
const preflightMessage = getPreflightStatusMessage({
|
|
32
|
+
authStatus,
|
|
33
|
+
requiresManualLogin: false,
|
|
34
|
+
canAutoRefreshLogin: true,
|
|
95
35
|
});
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
let transport = createTransport(tokens.access_token);
|
|
99
|
-
try {
|
|
100
|
-
await remoteClient.connect(transport);
|
|
36
|
+
if (preflightMessage) {
|
|
37
|
+
process.stderr.write(`${preflightMessage}\n\n`);
|
|
101
38
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
"Run:\n" +
|
|
110
|
-
`${getLoginCommand(serverUrl)}\n\n` +
|
|
111
|
-
"Then retry the same request.\n");
|
|
112
|
-
process.exit(1);
|
|
113
|
-
}
|
|
114
|
-
tokens = refreshed;
|
|
115
|
-
transport = createTransport(tokens.access_token);
|
|
116
|
-
await remoteClient.connect(transport);
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
119
|
-
throw error;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
// 3. 获取远程 server 的能力
|
|
123
|
-
const serverInfo = remoteClient.getServerVersion();
|
|
39
|
+
const remoteClient = new OAuthRemoteMcpClient({
|
|
40
|
+
serverUrl,
|
|
41
|
+
oauth: {
|
|
42
|
+
authorizationOutput: "stderr",
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
await remoteClient.connect();
|
|
124
46
|
const remoteTools = await remoteClient.listTools();
|
|
125
|
-
// 4. 创建本地 stdio server,代理所有请求
|
|
126
47
|
const localServer = new Server({
|
|
127
|
-
name:
|
|
128
|
-
version:
|
|
48
|
+
name: `${CLIENT_NAME}-proxy`,
|
|
49
|
+
version: CLIENT_VERSION,
|
|
129
50
|
}, {
|
|
130
51
|
capabilities: {
|
|
131
52
|
tools: { listChanged: false },
|
|
132
53
|
},
|
|
133
54
|
});
|
|
134
|
-
// 代理 tools/list
|
|
135
55
|
localServer.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
136
56
|
try {
|
|
137
|
-
|
|
138
|
-
return result;
|
|
57
|
+
return await remoteClient.listTools();
|
|
139
58
|
}
|
|
140
59
|
catch {
|
|
141
60
|
return { tools: remoteTools.tools };
|
|
142
61
|
}
|
|
143
62
|
});
|
|
144
|
-
// 代理 tools/call
|
|
145
63
|
localServer.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
146
64
|
const { name, arguments: args } = request.params;
|
|
147
|
-
return remoteClient.callTool(
|
|
65
|
+
return remoteClient.callTool(name, (args ?? {}));
|
|
148
66
|
});
|
|
149
|
-
// 代理 ping
|
|
150
67
|
localServer.setRequestHandler(PingRequestSchema, async () => {
|
|
151
68
|
await remoteClient.ping();
|
|
152
69
|
return {};
|
|
153
70
|
});
|
|
154
|
-
// 5. 启动 stdio transport
|
|
155
71
|
const stdioTransport = new StdioServerTransport();
|
|
156
72
|
await localServer.connect(stdioTransport);
|
|
157
73
|
process.stderr.write(`[crush-mcp-proxy] Connected to ${serverUrl} | stdio proxy ready\n`);
|
|
158
|
-
// 优雅关闭
|
|
159
74
|
const cleanup = async () => {
|
|
160
75
|
await localServer.close();
|
|
161
76
|
await remoteClient.close();
|
|
@@ -7,6 +7,8 @@ export type DoctorCheck = {
|
|
|
7
7
|
};
|
|
8
8
|
export declare const getTargetLabel: (target: SetupTarget) => string;
|
|
9
9
|
export declare const getLoginCommand: (serverUrl: string) => string;
|
|
10
|
+
export declare const formatLoginRequiredMessage: (serverUrl: string) => string;
|
|
11
|
+
export declare const formatAutomaticRefreshMessage: (serverUrl: string) => string;
|
|
10
12
|
export declare const formatSetupSummary: (results: SetupInstallResult[], scope: SetupScope) => string;
|
|
11
13
|
export declare const formatAuthStatus: (status: CachedAuthStatus) => string;
|
|
12
14
|
export declare const formatDoctorReport: (serverUrl: string, checks: DoctorCheck[]) => string;
|
|
@@ -14,6 +14,21 @@ export const getTargetLabel = (target) => TARGET_LABELS[target];
|
|
|
14
14
|
export const getLoginCommand = (serverUrl) => serverUrl === DEFAULT_MCP_SERVER_URL
|
|
15
15
|
? `npx -y ${PACKAGE_NAME} login`
|
|
16
16
|
: `CRUSH_MCP_SERVER_URL=\"${serverUrl}\" npx -y ${PACKAGE_NAME} login`;
|
|
17
|
+
export const formatLoginRequiredMessage = (serverUrl) => [
|
|
18
|
+
"Crush needs sign-in on this machine.",
|
|
19
|
+
"",
|
|
20
|
+
"Run this once in your terminal:",
|
|
21
|
+
getLoginCommand(serverUrl),
|
|
22
|
+
"",
|
|
23
|
+
"Then retry the same request.",
|
|
24
|
+
].join("\n");
|
|
25
|
+
export const formatAutomaticRefreshMessage = (serverUrl) => [
|
|
26
|
+
"Crush found previous local authorization data.",
|
|
27
|
+
"Refreshing the sign-in flow automatically...",
|
|
28
|
+
"",
|
|
29
|
+
"If the browser does not open, run:",
|
|
30
|
+
getLoginCommand(serverUrl),
|
|
31
|
+
].join("\n");
|
|
17
32
|
export const formatSetupSummary = (results, scope) => {
|
|
18
33
|
const targets = results.map((result) => getTargetLabel(result.target)).join(", ");
|
|
19
34
|
const lines = [
|
package/package.json
CHANGED
|
@@ -1,9 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crush-protocol/mcp-client",
|
|
3
|
-
"version": "0.4.
|
|
4
|
-
"description": "Crush MCP
|
|
3
|
+
"version": "0.4.4",
|
|
4
|
+
"description": "Official Crush MCP client for hosted market data, backtests, and trading workflows",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/crush-protocol/crush-mcp-server/tree/main/packages/crush-mcp-client#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/crush-protocol/crush-mcp-server.git",
|
|
11
|
+
"directory": "packages/crush-mcp-client"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/crush-protocol/crush-mcp-server/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"crush",
|
|
18
|
+
"crush-protocol",
|
|
19
|
+
"mcp",
|
|
20
|
+
"model-context-protocol",
|
|
21
|
+
"trading",
|
|
22
|
+
"backtest",
|
|
23
|
+
"quant",
|
|
24
|
+
"cursor",
|
|
25
|
+
"claude-code",
|
|
26
|
+
"codex"
|
|
27
|
+
],
|
|
7
28
|
"main": "dist/index.js",
|
|
8
29
|
"types": "dist/index.d.ts",
|
|
9
30
|
"bin": {
|
|
@@ -39,7 +60,7 @@
|
|
|
39
60
|
"node": ">=20"
|
|
40
61
|
},
|
|
41
62
|
"scripts": {
|
|
42
|
-
"build": "tsc -p tsconfig.json",
|
|
63
|
+
"build": "rm -rf dist && tsc -p tsconfig.json",
|
|
43
64
|
"dev": "tsx src/cli.ts",
|
|
44
65
|
"test": "vitest run",
|
|
45
66
|
"test:e2e": "dotenv -e .env.e2e vitest run src/__tests__/e2e.test.ts"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { formatAuthStatus, formatSetupSummary, getLoginCommand } from "../onboarding/cliOutput.js";
|
|
3
|
-
describe("cliOutput", () => {
|
|
4
|
-
it("renders setup summary with next steps", () => {
|
|
5
|
-
const results = [
|
|
6
|
-
{
|
|
7
|
-
target: "cursor",
|
|
8
|
-
scope: "user",
|
|
9
|
-
status: "created",
|
|
10
|
-
location: "/tmp/.cursor/mcp.json",
|
|
11
|
-
},
|
|
12
|
-
];
|
|
13
|
-
const output = formatSetupSummary(results, "user");
|
|
14
|
-
expect(output).toContain("Crush MCP configured for Cursor.");
|
|
15
|
-
expect(output).toContain("Restart Cursor");
|
|
16
|
-
expect(output).toContain("npx -y @crush-protocol/mcp-client login");
|
|
17
|
-
});
|
|
18
|
-
it("renders auth status with login guidance when missing", () => {
|
|
19
|
-
const output = formatAuthStatus({
|
|
20
|
-
serverUrl: "https://crush-mcp-ats.dev.xexlab.com/mcp",
|
|
21
|
-
status: "not_authenticated",
|
|
22
|
-
hasClientInformation: false,
|
|
23
|
-
hasAccessToken: false,
|
|
24
|
-
hasRefreshToken: false,
|
|
25
|
-
hasCodeVerifier: false,
|
|
26
|
-
});
|
|
27
|
-
expect(output).toContain("Status: Not authenticated");
|
|
28
|
-
expect(output).toContain("Run:");
|
|
29
|
-
expect(output).toContain("npx -y @crush-protocol/mcp-client login");
|
|
30
|
-
});
|
|
31
|
-
it("renders custom login command for non-default server URLs", () => {
|
|
32
|
-
expect(getLoginCommand("https://example.com/mcp")).toBe('CRUSH_MCP_SERVER_URL="https://example.com/mcp" npx -y @crush-protocol/mcp-client login');
|
|
33
|
-
});
|
|
34
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
2
|
-
import { BacktestClient } from "../backtest/backtestClient.js";
|
|
3
|
-
import { OAuthRemoteMcpClient } from "../mcp/oauthRemoteClient.js";
|
|
4
|
-
/**
|
|
5
|
-
* E2E 测试:验证 MCP client 能成功连接服务端并调用工具。
|
|
6
|
-
*
|
|
7
|
-
* 前置条件(通过环境变量或 .env.e2e 配置):
|
|
8
|
-
* CRUSH_MCP_SERVER_URL — 指向运行中的 MCP server(默认 https://crush-mcp-ats.dev.xexlab.com/mcp)
|
|
9
|
-
* CRUSH_OAUTH_ACCESS_TOKEN — 有效的 OAuth access token
|
|
10
|
-
*/
|
|
11
|
-
const serverUrl = process.env.CRUSH_MCP_SERVER_URL ?? "https://crush-mcp-ats.dev.xexlab.com/mcp";
|
|
12
|
-
const token = process.env.CRUSH_OAUTH_ACCESS_TOKEN ?? "";
|
|
13
|
-
// 没有 token 时跳过所有 e2e 测试
|
|
14
|
-
const describeE2E = token ? describe : describe.skip;
|
|
15
|
-
describeE2E("MCP Client E2E", () => {
|
|
16
|
-
let mcp;
|
|
17
|
-
let backtest;
|
|
18
|
-
beforeAll(async () => {
|
|
19
|
-
mcp = new OAuthRemoteMcpClient({ serverUrl, token, oauth: { openBrowser: false } });
|
|
20
|
-
await mcp.connect();
|
|
21
|
-
backtest = new BacktestClient(mcp);
|
|
22
|
-
});
|
|
23
|
-
afterAll(async () => {
|
|
24
|
-
await mcp.close();
|
|
25
|
-
});
|
|
26
|
-
it("ping — 服务端可达", async () => {
|
|
27
|
-
const result = await mcp.ping();
|
|
28
|
-
expect(result).toBeDefined();
|
|
29
|
-
});
|
|
30
|
-
it("listTools — 返回至少一个工具", async () => {
|
|
31
|
-
const { tools } = await mcp.listTools();
|
|
32
|
-
expect(tools.length).toBeGreaterThan(0);
|
|
33
|
-
});
|
|
34
|
-
it("backtest:schema — 返回支持的配置 schema", async () => {
|
|
35
|
-
const schema = await backtest.getConfigSchema();
|
|
36
|
-
expect(schema).toHaveProperty("platforms");
|
|
37
|
-
expect(schema).toHaveProperty("timeframes");
|
|
38
|
-
expect(Array.isArray(schema.platforms)).toBe(true);
|
|
39
|
-
});
|
|
40
|
-
it("backtest:tokens — 返回可用交易对列表", async () => {
|
|
41
|
-
const result = await backtest.getAvailableTokens();
|
|
42
|
-
expect(result).toHaveProperty("tokens");
|
|
43
|
-
expect(Array.isArray(result.tokens)).toBe(true);
|
|
44
|
-
});
|
|
45
|
-
it("backtest:list — 返回当前用户的回测列表", async () => {
|
|
46
|
-
const result = await backtest.list({ limit: 5 });
|
|
47
|
-
expect(result).toHaveProperty("backtests");
|
|
48
|
-
expect(typeof result.total).toBe("number");
|
|
49
|
-
});
|
|
50
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { mkdtemp, readFile } from "node:fs/promises";
|
|
2
|
-
import os from "node:os";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { afterEach, describe, expect, it } from "vitest";
|
|
5
|
-
import { InteractiveOAuthProvider } from "../mcp/oauthProvider.js";
|
|
6
|
-
const tempDirs = [];
|
|
7
|
-
const createProvider = async () => {
|
|
8
|
-
const storageDir = await mkdtemp(path.join(os.tmpdir(), "crush-mcp-oauth-"));
|
|
9
|
-
tempDirs.push(storageDir);
|
|
10
|
-
const provider = new InteractiveOAuthProvider({
|
|
11
|
-
serverUrl: "https://example.com/mcp",
|
|
12
|
-
storageDir,
|
|
13
|
-
openBrowser: false,
|
|
14
|
-
});
|
|
15
|
-
return { provider, storageDir };
|
|
16
|
-
};
|
|
17
|
-
describe("InteractiveOAuthProvider", () => {
|
|
18
|
-
afterEach(async () => {
|
|
19
|
-
await Promise.all(tempDirs
|
|
20
|
-
.splice(0)
|
|
21
|
-
.map((dir) => import("node:fs/promises").then((fs) => fs.rm(dir, { recursive: true, force: true }))));
|
|
22
|
-
});
|
|
23
|
-
it("persists client info, tokens, and code verifier", async () => {
|
|
24
|
-
const { provider, storageDir } = await createProvider();
|
|
25
|
-
await provider.saveClientInformation({ client_id: "client_123" });
|
|
26
|
-
await provider.saveTokens({ access_token: "atk_123", token_type: "Bearer", refresh_token: "rt_123" });
|
|
27
|
-
await provider.saveCodeVerifier("verifier-123");
|
|
28
|
-
expect(await provider.clientInformation()).toEqual({ client_id: "client_123" });
|
|
29
|
-
expect(await provider.tokens()).toEqual({ access_token: "atk_123", token_type: "Bearer", refresh_token: "rt_123" });
|
|
30
|
-
expect(await provider.codeVerifier()).toBe("verifier-123");
|
|
31
|
-
const files = await import("node:fs/promises").then((fs) => fs.readdir(storageDir));
|
|
32
|
-
expect(files.length).toBe(1);
|
|
33
|
-
const raw = await readFile(path.join(storageDir, files[0]), "utf8");
|
|
34
|
-
expect(raw).toContain("client_123");
|
|
35
|
-
expect(raw).toContain("atk_123");
|
|
36
|
-
});
|
|
37
|
-
it("invalidates token state without removing client registration", async () => {
|
|
38
|
-
const { provider } = await createProvider();
|
|
39
|
-
await provider.saveClientInformation({ client_id: "client_123" });
|
|
40
|
-
await provider.saveTokens({ access_token: "atk_123", token_type: "Bearer", refresh_token: "rt_123" });
|
|
41
|
-
await provider.invalidateCredentials?.("tokens");
|
|
42
|
-
expect(await provider.clientInformation()).toEqual({ client_id: "client_123" });
|
|
43
|
-
expect(await provider.tokens()).toBeUndefined();
|
|
44
|
-
});
|
|
45
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { mkdtemp, writeFile } from "node:fs/promises";
|
|
2
|
-
import os from "node:os";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { afterEach, describe, expect, it } from "vitest";
|
|
5
|
-
import { defaultStorageDir, getCachedAuthStatus, getStorageFileForServer, loadStoredTokens, } from "../mcp/oauthStorage.js";
|
|
6
|
-
const tempDirs = [];
|
|
7
|
-
describe("oauthStorage", () => {
|
|
8
|
-
afterEach(async () => {
|
|
9
|
-
await Promise.all(tempDirs
|
|
10
|
-
.splice(0)
|
|
11
|
-
.map((dir) => import("node:fs/promises").then((fs) => fs.rm(dir, { recursive: true, force: true }))));
|
|
12
|
-
});
|
|
13
|
-
it("reports authenticated state when tokens are present", async () => {
|
|
14
|
-
const storageDir = await mkdtemp(path.join(os.tmpdir(), "crush-mcp-storage-"));
|
|
15
|
-
tempDirs.push(storageDir);
|
|
16
|
-
const storageFile = getStorageFileForServer("https://example.com/mcp", storageDir);
|
|
17
|
-
await writeFile(storageFile, JSON.stringify({
|
|
18
|
-
clientInformation: { client_id: "client_123" },
|
|
19
|
-
tokens: { access_token: "atk_123", refresh_token: "rt_123", token_type: "Bearer", scope: "mcp:tools" },
|
|
20
|
-
}), "utf8");
|
|
21
|
-
const status = await getCachedAuthStatus("https://example.com/mcp", storageDir);
|
|
22
|
-
expect(status.status).toBe("authenticated");
|
|
23
|
-
expect(status.hasRefreshToken).toBe(true);
|
|
24
|
-
expect(status.storageFile).toBe(storageFile);
|
|
25
|
-
});
|
|
26
|
-
it("falls back between /mcp and base URL variants", async () => {
|
|
27
|
-
const storageDir = await mkdtemp(path.join(os.tmpdir(), "crush-mcp-storage-"));
|
|
28
|
-
tempDirs.push(storageDir);
|
|
29
|
-
const storageFile = getStorageFileForServer("https://example.com", storageDir);
|
|
30
|
-
await writeFile(storageFile, JSON.stringify({
|
|
31
|
-
clientInformation: { client_id: "client_123" },
|
|
32
|
-
tokens: { access_token: "atk_123", token_type: "Bearer" },
|
|
33
|
-
}), "utf8");
|
|
34
|
-
const tokens = await loadStoredTokens("https://example.com/mcp", storageDir);
|
|
35
|
-
expect(tokens?.access_token).toBe("atk_123");
|
|
36
|
-
});
|
|
37
|
-
it("reports registered state when only client metadata exists", async () => {
|
|
38
|
-
const storageDir = await mkdtemp(path.join(os.tmpdir(), "crush-mcp-storage-"));
|
|
39
|
-
tempDirs.push(storageDir);
|
|
40
|
-
const storageFile = getStorageFileForServer("https://example.com/mcp", storageDir);
|
|
41
|
-
await writeFile(storageFile, JSON.stringify({
|
|
42
|
-
clientInformation: { client_id: "client_123" },
|
|
43
|
-
codeVerifier: "verifier_123",
|
|
44
|
-
}), "utf8");
|
|
45
|
-
const status = await getCachedAuthStatus("https://example.com/mcp", storageDir);
|
|
46
|
-
expect(status.status).toBe("registered");
|
|
47
|
-
expect(status.hasCodeVerifier).toBe(true);
|
|
48
|
-
});
|
|
49
|
-
it("uses the default storage directory helper", () => {
|
|
50
|
-
expect(defaultStorageDir()).toContain(".crush-mcp");
|
|
51
|
-
});
|
|
52
|
-
});
|