Skip to main content
Glama
by Cytrogen

read_file_content

Retrieve content from specified project files using a prefixed path, enabling direct access to local code repositories for analysis and integration.

Instructions

读取项目内指定文件的内容,文件路径必须包含前缀,例如 '[backend/src]/main.ts'

Input Schema

NameRequiredDescriptionDefault
filePathYes带前缀的完整文件路径, e.g., '[backend/src]/main.ts'

Input Schema (JSON Schema)

{ "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": false, "properties": { "filePath": { "description": "带前缀的完整文件路径, e.g., '[backend/src]/main.ts'", "type": "string" } }, "required": [ "filePath" ], "type": "object" }

Implementation Reference

  • The handler function for the 'read_file_content' tool. It parses the filePath with prefix, resolves the absolute path using pathRegistry, performs security checks, reads the file content using fs.readFile, and returns it formatted in the MCP response format.
    async ({ filePath }) => { const match = filePath.match(/^(\[.*?\])\/(.*)$/s); if (!match) return { content: [{ type: "text", text: "错误:文件路径格式不正确,必须包含如 '[backend/src]/' 的前缀。" }] }; const prefix = match[1]; const relativePath = match[2]; const rootPath = pathRegistry.get(prefix); if (!rootPath) return { content: [{ type: "text", text: `错误:未知的路径前缀 '${prefix}'。` }] }; const resolvedPath = path.resolve(rootPath, relativePath); if (!resolvedPath.startsWith(path.resolve(rootPath))) return { content: [{ type: "text", text: "错误:禁止访问项目目录之外的文件。" }] }; try { const fileContent = await fs.readFile(resolvedPath, "utf-8"); return { content: [{ type: "text", text: `文件 '${filePath}' 的内容:\n---\n${fileContent}` }] }; } catch (error: any) { let errorMessage = `读取文件 '${filePath}' 时发生错误。`; if (error.code === 'ENOENT') errorMessage = `错误:文件 '${filePath}' 未找到。`; console.error(errorMessage, error); return { content: [{ type: "text", text: errorMessage }] }; } }
  • The input schema for the tool using Zod, defining the 'filePath' parameter.
    { filePath: z.string().describe("带前缀的完整文件路径, e.g., '[backend/src]/main.ts'"), },
  • src/index.ts:269-298 (registration)
    Registration of the 'read_file_content' tool using McpServer.tool() method, including name, description, schema, and handler.
    server.tool( "read_file_content", "读取项目内指定文件的内容,文件路径必须包含前缀,例如 '[backend/src]/main.ts'", { filePath: z.string().describe("带前缀的完整文件路径, e.g., '[backend/src]/main.ts'"), }, async ({ filePath }) => { const match = filePath.match(/^(\[.*?\])\/(.*)$/s); if (!match) return { content: [{ type: "text", text: "错误:文件路径格式不正确,必须包含如 '[backend/src]/' 的前缀。" }] }; const prefix = match[1]; const relativePath = match[2]; const rootPath = pathRegistry.get(prefix); if (!rootPath) return { content: [{ type: "text", text: `错误:未知的路径前缀 '${prefix}'。` }] }; const resolvedPath = path.resolve(rootPath, relativePath); if (!resolvedPath.startsWith(path.resolve(rootPath))) return { content: [{ type: "text", text: "错误:禁止访问项目目录之外的文件。" }] }; try { const fileContent = await fs.readFile(resolvedPath, "utf-8"); return { content: [{ type: "text", text: `文件 '${filePath}' 的内容:\n---\n${fileContent}` }] }; } catch (error: any) { let errorMessage = `读取文件 '${filePath}' 时发生错误。`; if (error.code === 'ENOENT') errorMessage = `错误:文件 '${filePath}' 未找到。`; console.error(errorMessage, error); return { content: [{ type: "text", text: errorMessage }] }; } } );

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Cytrogen/local-project-sync'

If you have feedback or need assistance with the MCP directory API, please join our Discord server