mirror of
https://github.com/Yidadaa/ChatGPT-Next-Web.git
synced 2025-08-08 19:52:27 +08:00
feat: simple MCP example
This commit is contained in:
92
app/mcp/example.ts
Normal file
92
app/mcp/example.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import { createClient, listPrimitives } from "@/app/mcp/client";
|
||||
import { MCPClientLogger } from "@/app/mcp/logger";
|
||||
import { z } from "zod";
|
||||
import { MCP_CONF } from "@/app/mcp/mcp_config";
|
||||
|
||||
const logger = new MCPClientLogger("MCP FS Example", true);
|
||||
|
||||
const ListAllowedDirectoriesResultSchema = z.object({
|
||||
content: z.array(
|
||||
z.object({
|
||||
type: z.string(),
|
||||
text: z.string(),
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
const ReadFileResultSchema = z.object({
|
||||
content: z.array(
|
||||
z.object({
|
||||
type: z.string(),
|
||||
text: z.string(),
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
async function main() {
|
||||
logger.info("Connecting to server...");
|
||||
|
||||
const client = await createClient(MCP_CONF.filesystem, "fs");
|
||||
const primitives = await listPrimitives(client);
|
||||
|
||||
logger.success(`Connected to server fs`);
|
||||
|
||||
logger.info(
|
||||
`server capabilities: ${Object.keys(
|
||||
client.getServerCapabilities() ?? [],
|
||||
).join(", ")}`,
|
||||
);
|
||||
|
||||
logger.debug("Server supports the following primitives:");
|
||||
|
||||
primitives.forEach((primitive) => {
|
||||
logger.debug("\n" + JSON.stringify(primitive, null, 2));
|
||||
});
|
||||
|
||||
const listAllowedDirectories = async () => {
|
||||
const result = await client.request(
|
||||
{
|
||||
method: "tools/call",
|
||||
params: {
|
||||
name: "list_allowed_directories",
|
||||
arguments: {},
|
||||
},
|
||||
},
|
||||
ListAllowedDirectoriesResultSchema,
|
||||
);
|
||||
logger.success(`Allowed directories: ${result.content[0].text}`);
|
||||
return result;
|
||||
};
|
||||
|
||||
const readFile = async (path: string) => {
|
||||
const result = await client.request(
|
||||
{
|
||||
method: "tools/call",
|
||||
params: {
|
||||
name: "read_file",
|
||||
arguments: {
|
||||
path: path,
|
||||
},
|
||||
},
|
||||
},
|
||||
ReadFileResultSchema,
|
||||
);
|
||||
logger.success(`File contents for ${path}:\n${result.content[0].text}`);
|
||||
return result;
|
||||
};
|
||||
|
||||
try {
|
||||
logger.info("Example 1: List allowed directories\n");
|
||||
await listAllowedDirectories();
|
||||
|
||||
logger.info("\nExample 2: Read a file\n");
|
||||
await readFile("/users/kadxy/desktop/test.txt");
|
||||
} catch (error) {
|
||||
logger.error(`Error executing examples: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
logger.error(error);
|
||||
process.exit(1);
|
||||
});
|
Reference in New Issue
Block a user