Skip to content

Commit b1ed056

Browse files
committed
w
1 parent db58bd2 commit b1ed056

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

app/components/mcp-market.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
resumeMcpServer,
2323
} from "../mcp/actions";
2424
import {
25-
ListToolsResponse,
2625
ToolSchema,
2726
McpConfigData,
2827
PresetServer,
@@ -48,7 +47,7 @@ export function McpMarketPage() {
4847
const [searchText, setSearchText] = useState("");
4948
const [userConfig, setUserConfig] = useState<Record<string, any>>({});
5049
const [editingServerId, setEditingServerId] = useState<string | undefined>();
51-
const [tools, setTools] = useState<ListToolsResponse | null>(null);
50+
const [tools, setTools] = useState<ToolSchema[] | null>(null);
5251
const [viewingServerId, setViewingServerId] = useState<string | undefined>();
5352
const [isLoading, setIsLoading] = useState(false);
5453
const [config, setConfig] = useState<McpConfigData>();
@@ -232,7 +231,7 @@ export function McpMarketPage() {
232231
try {
233232
const result = await getClientTools(id);
234233
if (result) {
235-
setTools(result);
234+
setTools(result?.tools);
236235
} else {
237236
throw new Error("Failed to load tools");
238237
}
@@ -733,8 +732,8 @@ export function McpMarketPage() {
733732
<div className={styles["tools-list"]}>
734733
{isLoading ? (
735734
<div>Loading...</div>
736-
) : tools?.tools ? (
737-
tools.tools.map((tool: ToolSchema, index: number) => (
735+
) : tools ? (
736+
tools.map((tool: ToolSchema, index: number) => (
738737
<div key={index} className={styles["tool-item"]}>
739738
<div className={styles["tool-name"]}>{tool.name}</div>
740739
<div className={styles["tool-description"]}>

app/mcp/actions.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
ServerStatusResponse,
1818
} from "./types";
1919

20+
const JSON_INDENT = 2;
21+
2022
const logger = new MCPClientLogger("MCP Actions");
2123

2224
const getConfigPath = async () => {
@@ -29,6 +31,7 @@ const getConfigPath = async () => {
2931
};
3032

3133
const clientsMap = new Map<string, McpClientData>();
34+
const toolToClientMap = new Map<string, string>();
3235

3336
// 获取客户端状态
3437
export async function getClientsStatus(): Promise<
@@ -133,6 +136,13 @@ async function initializeSingleClient(
133136
`Supported tools for [${clientId}]: ${JSON.stringify(tools, null, 2)}`,
134137
);
135138
clientsMap.set(clientId, { client, tools, errorMsg: null });
139+
if (tools?.tools) {
140+
for (const tool of tools.tools) {
141+
if (tool.name) {
142+
toolToClientMap.set(tool.name, clientId);
143+
}
144+
}
145+
}
136146
logger.success(`Client [${clientId}] initialized successfully`);
137147
})
138148
.catch((error) => {
@@ -250,6 +260,13 @@ export async function resumeMcpServer(clientId: string): Promise<void> {
250260
const client = await createClient(clientId, serverConfig);
251261
const tools = await listTools(client);
252262
clientsMap.set(clientId, { client, tools, errorMsg: null });
263+
if (tools?.tools) {
264+
for (const tool of tools.tools) {
265+
if (tool.name) {
266+
toolToClientMap.set(tool.name, clientId);
267+
}
268+
}
269+
}
253270
logger.success(`Client [${clientId}] initialized successfully`);
254271

255272
// 初始化成功后更新配置
@@ -354,9 +371,10 @@ export async function executeMcpAction(
354371
) {
355372
// Use a tool-to-client mapping that's maintained when tools are initialized
356373
const toolName = request.params.name;
357-
client = [...clientsMap.values()].find(
358-
(c) => c.tools?.tools && c.tools.tools.some((t) => t.name == toolName),
359-
);
374+
const toolClientId = toolToClientMap.get(toolName);
375+
if (toolClientId) {
376+
client = clientsMap.get(toolClientId);
377+
}
360378
}
361379
if (!client?.client) {
362380
throw new Error(`Client ${clientId} not found`);
@@ -417,7 +435,10 @@ async function updateMcpConfig(config: McpConfigData): Promise<void> {
417435
const path = await import("path");
418436
// 确保目录存在
419437
await fs.mkdir(path.dirname(await getConfigPath()), { recursive: true });
420-
await fs.writeFile(await getConfigPath(), JSON.stringify(config, null, 2));
438+
await fs.writeFile(
439+
await getConfigPath(),
440+
JSON.stringify(config, null, JSON_INDENT),
441+
);
421442
}
422443
}
423444

app/mcp/client.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export async function createClient(
2525
transport = new SSEClientTransport(new URL(config.url));
2626
} else {
2727
if (EXPORT_MODE) {
28-
throw new Error("Cannot use stdio transport in export mode");
28+
throw new Error(
29+
"Cannot use stdio transport in export mode. Please use SSE transport configuration instead.",
30+
);
2931
} else {
3032
const { StdioClientTransport } = await import(
3133
"@modelcontextprotocol/sdk/client/stdio.js"

0 commit comments

Comments
 (0)