Added Zod

This commit is contained in:
Mahesh Murag 2024-11-20 22:45:22 -05:00
commit f9041bbced
No known key found for this signature in database

View file

@ -4,46 +4,51 @@ import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { import {
CallToolRequestSchema, CallToolRequestSchema,
ListResourcesRequestSchema,
ListToolsRequestSchema, ListToolsRequestSchema,
ReadResourceRequestSchema, ToolSchema,
} from "@modelcontextprotocol/sdk/types.js"; } from "@modelcontextprotocol/sdk/types.js";
import fs from "fs/promises"; import fs from "fs/promises";
import path from "path"; import path from "path";
import { promisify } from "util"; import { z } from "zod";
import { exec as execCallback } from "child_process"; import { zodToJsonSchema } from "zod-to-json-schema";
// Define interfaces for the tool arguments const ReadFileArgsSchema = z.object({
interface ReadFileArgs { path: z.string(),
path: string; });
}
interface ReadMultipleFilesArgs { const ReadMultipleFilesArgsSchema = z.object({
paths: string[]; paths: z.array(z.string()),
} });
interface WriteFileArgs { const WriteFileArgsSchema = z.object({
path: string; path: z.string(),
content: string; content: z.string(),
} });
interface CreateDirectoryArgs { const CreateDirectoryArgsSchema = z.object({
path: string; path: z.string(),
} });
interface ListDirectoryArgs { const ListDirectoryArgsSchema = z.object({
path: string; path: z.string(),
} });
interface MoveFileArgs { const MoveFileArgsSchema = z.object({
source: string; source: z.string(),
destination: string; destination: z.string(),
} });
interface SearchFilesArgs { const SearchFilesArgsSchema = z.object({
path: string; path: z.string(),
pattern: string; pattern: z.string(),
} });
const GetFileInfoArgsSchema = z.object({
path: z.string(),
});
const ToolInputSchema = ToolSchema.shape.inputSchema;
type ToolInput = z.infer<typeof ToolInputSchema>;
interface FileInfo { interface FileInfo {
size: number; size: number;
@ -55,8 +60,6 @@ interface FileInfo {
permissions: string; permissions: string;
} }
const exec = promisify(execCallback);
const server = new Server( const server = new Server(
{ {
name: "example-servers/filesystem", name: "example-servers/filesystem",
@ -65,215 +68,56 @@ const server = new Server(
{ {
capabilities: { capabilities: {
tools: {}, tools: {},
resources: {}, // Need this since we're using resources
}, },
}, },
); );
// Add Resources List Handler
server.setRequestHandler(ListResourcesRequestSchema, async () => {
return {
resources: [
{
uri: "file://system",
mimeType: "text/plain",
name: "File System Operations",
},
],
};
});
// Add Read Resource Handler
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
if (request.params.uri.toString() === "file://system") {
return {
contents: [
{
uri: "file://system",
mimeType: "text/plain",
text: "File system operations interface",
},
],
};
}
throw new Error("Resource not found");
});
server.setRequestHandler(ListToolsRequestSchema, async () => { server.setRequestHandler(ListToolsRequestSchema, async () => {
return { return {
tools: [ tools: [
{ {
name: "read_file", name: "read_file",
description: description: "Read the complete contents of a file from the file system.",
"Read the complete contents of a file from the file system. " + inputSchema: zodToJsonSchema(ReadFileArgsSchema) as ToolInput,
"Handles various text encodings and provides detailed error messages " +
"if the file cannot be read. Use this tool when you need to examine " +
"the contents of a single file.",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description:
"Absolute or relative path to the file you want to read",
},
},
required: ["path"],
},
}, },
{ {
name: "read_multiple_files", name: "read_multiple_files",
description: description: "Read the contents of multiple files simultaneously.",
"Read the contents of multiple files simultaneously. This is more " + inputSchema: zodToJsonSchema(ReadMultipleFilesArgsSchema) as ToolInput,
"efficient than reading files one by one when you need to analyze " +
"or compare multiple files. Each file's content is returned with its " +
"path as a reference. Failed reads for individual files won't stop " +
"the entire operation.",
inputSchema: {
type: "object",
properties: {
paths: {
type: "array",
items: {
type: "string",
},
description:
"List of file paths to read. Can be absolute or relative paths.",
},
},
required: ["paths"],
},
}, },
{ {
name: "write_file", name: "write_file",
description: description: "Create a new file or completely overwrite an existing file with new content.",
"Create a new file or completely overwrite an existing file with new content. " + inputSchema: zodToJsonSchema(WriteFileArgsSchema) as ToolInput,
"Use with caution as it will overwrite existing files without warning. " +
"Handles text content with proper encoding.",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description:
"Path where the file should be written. Parent directories will be created if needed.",
},
content: {
type: "string",
description:
"Content to write to the file. Can include newlines and special characters.",
},
},
required: ["path", "content"],
},
}, },
{ {
name: "create_directory", name: "create_directory",
description: description: "Create a new directory or ensure a directory exists.",
"Create a new directory or ensure a directory exists. Can create multiple " + inputSchema: zodToJsonSchema(CreateDirectoryArgsSchema) as ToolInput,
"nested directories in one operation. If the directory already exists, " +
"this operation will succeed silently. Perfect for setting up directory " +
"structures for projects or ensuring required paths exist.",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description:
"Path of the directory to create. Will create parent directories if they don't exist.",
},
},
required: ["path"],
},
}, },
{ {
name: "list_directory", name: "list_directory",
description: description: "Get a detailed listing of all files and directories in a specified path.",
"Get a detailed listing of all files and directories in a specified path. " + inputSchema: zodToJsonSchema(ListDirectoryArgsSchema) as ToolInput,
"Results clearly distinguish between files and directories with [FILE] and [DIR] " +
"prefixes. This tool is essential for understanding directory structure and " +
"finding specific files within a directory.",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description:
"Path of the directory to list. Must be an existing directory.",
},
},
required: ["path"],
},
}, },
{ {
name: "move_file", name: "move_file",
description: description: "Move or rename files and directories.",
"Move or rename files and directories. Can move files between directories " + inputSchema: zodToJsonSchema(MoveFileArgsSchema) as ToolInput,
"and rename them in a single operation. If the destination exists, the " +
"operation will fail. Works across different directories and can be used " +
"for simple renaming within the same directory.",
inputSchema: {
type: "object",
properties: {
source: {
type: "string",
description: "Current path of the file or directory",
},
destination: {
type: "string",
description:
"New path where the file or directory should be moved to",
},
},
required: ["source", "destination"],
},
}, },
{ {
name: "search_files", name: "search_files",
description: description: "Recursively search for files and directories matching a pattern.",
"Recursively search for files and directories matching a pattern. " + inputSchema: zodToJsonSchema(SearchFilesArgsSchema) as ToolInput,
"Searches through all subdirectories from the starting path. The search " +
"is case-insensitive and matches partial names. Returns full paths to all " +
"matching items. Great for finding files when you don't know their exact location.",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description: "Starting directory for the search",
},
pattern: {
type: "string",
description:
"Text pattern to search for in file and directory names",
},
},
required: ["path", "pattern"],
},
}, },
{ {
name: "get_file_info", name: "get_file_info",
description: description: "Retrieve detailed metadata about a file or directory.",
"Retrieve detailed metadata about a file or directory. Returns comprehensive " + inputSchema: zodToJsonSchema(GetFileInfoArgsSchema) as ToolInput,
"information including size, creation time, last modified time, permissions, " +
"and type. This tool is perfect for understanding file characteristics " +
"without reading the actual content.",
inputSchema: {
type: "object",
properties: {
path: {
type: "string",
description:
"Path to the file or directory to get information about",
},
},
required: ["path"],
},
}, },
], ],
}; };
}); });
async function getFileStats(filePath: string): Promise<FileInfo> { async function getFileStats(filePath: string): Promise<FileInfo> {
const stats = await fs.stat(filePath); const stats = await fs.stat(filePath);
return { return {
@ -313,106 +157,34 @@ async function searchFiles(
return results; return results;
} }
// Add type guard functions for each argument type
function isReadFileArgs(args: unknown): args is ReadFileArgs {
return (
typeof args === "object" &&
args !== null &&
"path" in args &&
typeof (args as ReadFileArgs).path === "string"
);
}
function isReadMultipleFilesArgs(args: unknown): args is ReadMultipleFilesArgs {
return (
typeof args === "object" &&
args !== null &&
"paths" in args &&
Array.isArray((args as ReadMultipleFilesArgs).paths) &&
(args as ReadMultipleFilesArgs).paths.every(
(path) => typeof path === "string",
)
);
}
function isWriteFileArgs(args: unknown): args is WriteFileArgs {
return (
typeof args === "object" &&
args !== null &&
"path" in args &&
"content" in args &&
typeof (args as WriteFileArgs).path === "string" &&
typeof (args as WriteFileArgs).content === "string"
);
}
function isCreateDirectoryArgs(args: unknown): args is CreateDirectoryArgs {
return (
typeof args === "object" &&
args !== null &&
"path" in args &&
typeof (args as CreateDirectoryArgs).path === "string"
);
}
function isListDirectoryArgs(args: unknown): args is ListDirectoryArgs {
return (
typeof args === "object" &&
args !== null &&
"path" in args &&
typeof (args as ListDirectoryArgs).path === "string"
);
}
function isMoveFileArgs(args: unknown): args is MoveFileArgs {
return (
typeof args === "object" &&
args !== null &&
"source" in args &&
"destination" in args &&
typeof (args as MoveFileArgs).source === "string" &&
typeof (args as MoveFileArgs).destination === "string"
);
}
function isSearchFilesArgs(args: unknown): args is SearchFilesArgs {
return (
typeof args === "object" &&
args !== null &&
"path" in args &&
"pattern" in args &&
typeof (args as SearchFilesArgs).path === "string" &&
typeof (args as SearchFilesArgs).pattern === "string"
);
}
server.setRequestHandler(CallToolRequestSchema, async (request) => { server.setRequestHandler(CallToolRequestSchema, async (request) => {
try { try {
const { name, arguments: args } = request.params; const { name, arguments: args } = request.params;
switch (name) { switch (name) {
case "read_file": { case "read_file": {
if (!isReadFileArgs(args)) { const parsed = ReadFileArgsSchema.safeParse(args);
throw new Error("Invalid arguments for read_file"); if (!parsed.success) {
throw new Error(`Invalid arguments for read_file: ${parsed.error}`);
} }
const content = await fs.readFile(args.path, "utf-8"); const content = await fs.readFile(parsed.data.path, "utf-8");
return { return {
content: [{ type: "text", text: content }], content: [{ type: "text", text: content }],
}; };
} }
case "read_multiple_files": { case "read_multiple_files": {
if (!isReadMultipleFilesArgs(args)) { const parsed = ReadMultipleFilesArgsSchema.safeParse(args);
throw new Error("Invalid arguments for read_multiple_files"); if (!parsed.success) {
throw new Error(`Invalid arguments for read_multiple_files: ${parsed.error}`);
} }
const results = await Promise.all( const results = await Promise.all(
args.paths.map(async (filePath: string) => { parsed.data.paths.map(async (filePath: string) => {
try { try {
const content = await fs.readFile(filePath, "utf-8"); const content = await fs.readFile(filePath, "utf-8");
return `${filePath}:\n${content}\n`; return `${filePath}:\n${content}\n`;
} catch (error) { } catch (error) {
const errorMessage = const errorMessage = error instanceof Error ? error.message : String(error);
error instanceof Error ? error.message : String(error);
return `${filePath}: Error - ${errorMessage}`; return `${filePath}: Error - ${errorMessage}`;
} }
}), }),
@ -423,42 +195,35 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
} }
case "write_file": { case "write_file": {
if (!isWriteFileArgs(args)) { const parsed = WriteFileArgsSchema.safeParse(args);
throw new Error("Invalid arguments for write_file"); if (!parsed.success) {
throw new Error(`Invalid arguments for write_file: ${parsed.error}`);
} }
await fs.writeFile(args.path, args.content, "utf-8"); await fs.writeFile(parsed.data.path, parsed.data.content, "utf-8");
return { return {
content: [ content: [{ type: "text", text: `Successfully wrote to ${parsed.data.path}` }],
{ type: "text", text: `Successfully wrote to ${args.path}` },
],
}; };
} }
case "create_directory": { case "create_directory": {
if (!isCreateDirectoryArgs(args)) { const parsed = CreateDirectoryArgsSchema.safeParse(args);
throw new Error("Invalid arguments for create_directory"); if (!parsed.success) {
throw new Error(`Invalid arguments for create_directory: ${parsed.error}`);
} }
await fs.mkdir(args.path, { recursive: true }); await fs.mkdir(parsed.data.path, { recursive: true });
return { return {
content: [ content: [{ type: "text", text: `Successfully created directory ${parsed.data.path}` }],
{
type: "text",
text: `Successfully created directory ${args.path}`,
},
],
}; };
} }
case "list_directory": { case "list_directory": {
if (!isListDirectoryArgs(args)) { const parsed = ListDirectoryArgsSchema.safeParse(args);
throw new Error("Invalid arguments for list_directory"); if (!parsed.success) {
throw new Error(`Invalid arguments for list_directory: ${parsed.error}`);
} }
const entries = await fs.readdir(args.path, { withFileTypes: true }); const entries = await fs.readdir(parsed.data.path, { withFileTypes: true });
const formatted = entries const formatted = entries
.map( .map((entry) => `${entry.isDirectory() ? "[DIR]" : "[FILE]"} ${entry.name}`)
(entry) =>
`${entry.isDirectory() ? "[DIR]" : "[FILE]"} ${entry.name}`,
)
.join("\n"); .join("\n");
return { return {
content: [{ type: "text", text: formatted }], content: [{ type: "text", text: formatted }],
@ -466,50 +231,37 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
} }
case "move_file": { case "move_file": {
if (!isMoveFileArgs(args)) { const parsed = MoveFileArgsSchema.safeParse(args);
throw new Error("Invalid arguments for move_file"); if (!parsed.success) {
throw new Error(`Invalid arguments for move_file: ${parsed.error}`);
} }
await fs.rename(args.source, args.destination); await fs.rename(parsed.data.source, parsed.data.destination);
return { return {
content: [ content: [{ type: "text", text: `Successfully moved ${parsed.data.source} to ${parsed.data.destination}` }],
{
type: "text",
text: `Successfully moved ${args.source} to ${args.destination}`,
},
],
}; };
} }
case "search_files": { case "search_files": {
if (!isSearchFilesArgs(args)) { const parsed = SearchFilesArgsSchema.safeParse(args);
throw new Error("Invalid arguments for search_files"); if (!parsed.success) {
throw new Error(`Invalid arguments for search_files: ${parsed.error}`);
} }
const results = await searchFiles(args.path, args.pattern); const results = await searchFiles(parsed.data.path, parsed.data.pattern);
return { return {
content: [ content: [{ type: "text", text: results.length > 0 ? results.join("\n") : "No matches found" }],
{
type: "text",
text:
results.length > 0 ? results.join("\n") : "No matches found",
},
],
}; };
} }
case "get_file_info": { case "get_file_info": {
if (!isCreateDirectoryArgs(args)) { const parsed = GetFileInfoArgsSchema.safeParse(args);
throw new Error("Invalid arguments for get_file_info"); if (!parsed.success) {
throw new Error(`Invalid arguments for get_file_info: ${parsed.error}`);
} }
const info = await getFileStats(args.path); const info = await getFileStats(parsed.data.path);
return { return {
content: [ content: [{ type: "text", text: Object.entries(info)
{ .map(([key, value]) => `${key}: ${value}`)
type: "text", .join("\n") }],
text: Object.entries(info)
.map(([key, value]) => `${key}: ${value}`)
.join("\n"),
},
],
}; };
} }