mirror of
https://github.com/Yidadaa/ChatGPT-Next-Web.git
synced 2025-08-08 07:10:16 +08:00
feat: carry mcp primitives content as a system prompt
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
"use server";
|
||||
|
||||
import { createClient, executeRequest } from "./client";
|
||||
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
||||
import {
|
||||
createClient,
|
||||
executeRequest,
|
||||
listPrimitives,
|
||||
Primitive,
|
||||
} from "./client";
|
||||
import { MCPClientLogger } from "./logger";
|
||||
import conf from "./mcp_config.json";
|
||||
import { McpRequestMessage } from "./types";
|
||||
@@ -8,7 +13,10 @@ import { McpRequestMessage } from "./types";
|
||||
const logger = new MCPClientLogger("MCP Actions");
|
||||
|
||||
// Use Map to store all clients
|
||||
const clientsMap = new Map<string, any>();
|
||||
const clientsMap = new Map<
|
||||
string,
|
||||
{ client: Client; primitives: Primitive[] }
|
||||
>();
|
||||
|
||||
// Whether initialized
|
||||
let initialized = false;
|
||||
@@ -30,8 +38,11 @@ export async function initializeMcpClients() {
|
||||
try {
|
||||
logger.info(`Initializing MCP client: ${clientId}`);
|
||||
const client = await createClient(config, clientId);
|
||||
clientsMap.set(clientId, client);
|
||||
logger.success(`Client ${clientId} initialized`);
|
||||
const primitives = await listPrimitives(client);
|
||||
clientsMap.set(clientId, { client, primitives });
|
||||
logger.success(
|
||||
`Client [${clientId}] initialized, ${primitives.length} primitives supported`,
|
||||
);
|
||||
} catch (error) {
|
||||
errorClients.push(clientId);
|
||||
logger.error(`Failed to initialize client ${clientId}: ${error}`);
|
||||
@@ -58,7 +69,7 @@ export async function executeMcpAction(
|
||||
) {
|
||||
try {
|
||||
// Find the corresponding client
|
||||
const client = clientsMap.get(clientId);
|
||||
const client = clientsMap.get(clientId)?.client;
|
||||
if (!client) {
|
||||
logger.error(`Client ${clientId} not found`);
|
||||
return;
|
||||
@@ -80,3 +91,16 @@ export async function getAvailableClients() {
|
||||
(clientId) => !errorClients.includes(clientId),
|
||||
);
|
||||
}
|
||||
|
||||
// Get all primitives from all clients
|
||||
export async function getAllPrimitives(): Promise<
|
||||
{
|
||||
clientId: string;
|
||||
primitives: Primitive[];
|
||||
}[]
|
||||
> {
|
||||
return Array.from(clientsMap.entries()).map(([clientId, { primitives }]) => ({
|
||||
clientId,
|
||||
primitives,
|
||||
}));
|
||||
}
|
||||
|
@@ -40,13 +40,13 @@ export async function createClient(
|
||||
return client;
|
||||
}
|
||||
|
||||
interface Primitive {
|
||||
export interface Primitive {
|
||||
type: "resource" | "tool" | "prompt";
|
||||
value: any;
|
||||
}
|
||||
|
||||
/** List all resources, tools, and prompts */
|
||||
export async function listPrimitives(client: Client) {
|
||||
export async function listPrimitives(client: Client): Promise<Primitive[]> {
|
||||
const capabilities = client.getServerCapabilities();
|
||||
const primitives: Primitive[] = [];
|
||||
const promises = [];
|
||||
|
@@ -4,25 +4,25 @@ import conf from "./mcp_config.json";
|
||||
|
||||
const logger = new MCPClientLogger("MCP Server Example", true);
|
||||
|
||||
async function main() {
|
||||
logger.info("Connecting to server...");
|
||||
const TEST_SERVER = "everything";
|
||||
|
||||
const client = await createClient(conf.mcpServers.everything, "everything");
|
||||
async function main() {
|
||||
logger.info(`All MCP servers: ${Object.keys(conf.mcpServers).join(", ")}`);
|
||||
|
||||
logger.info(`Connecting to server ${TEST_SERVER}...`);
|
||||
|
||||
const client = await createClient(conf.mcpServers[TEST_SERVER], TEST_SERVER);
|
||||
const primitives = await listPrimitives(client);
|
||||
|
||||
logger.success(`Connected to server everything`);
|
||||
logger.success(`Connected to server ${TEST_SERVER}`);
|
||||
|
||||
logger.info(
|
||||
`server capabilities: ${Object.keys(
|
||||
client.getServerCapabilities() ?? [],
|
||||
).join(", ")}`,
|
||||
`${TEST_SERVER} supported primitives:\n${JSON.stringify(
|
||||
primitives.filter((i) => i.type === "tool"),
|
||||
null,
|
||||
2,
|
||||
)}`,
|
||||
);
|
||||
|
||||
logger.info("Server supports the following primitives:");
|
||||
|
||||
primitives.forEach((primitive) => {
|
||||
logger.info("\n" + JSON.stringify(primitive, null, 2));
|
||||
});
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
|
Reference in New Issue
Block a user