Skip to main content
Glama
p-united
by p-united

write_file

Write text content to a specified file within secure directories, enabling file creation and modification with controlled access permissions.

Instructions

指定されたファイルにテキストを書き込みます(許可されたディレクトリのみ)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filepathYes書き込むファイルのパス
contentYes書き込む内容

Implementation Reference

  • The core handler function for the 'write_file' tool. Validates the filepath using PathValidator, checks file extension, creates parent directory if necessary, writes content using fs.promises.writeFile, and returns a success CallToolResult.
    private async writeFile(filepath: string, content: string): Promise<CallToolResult> {
      try {
        const pathValidation = this.pathValidator.validatePath(filepath);
        if (!pathValidation.isValid) {
          throw new Error(pathValidation.error);
        }
    
        const extValidation = this.pathValidator.validateFileExtension(pathValidation.normalizedPath);
        if (!extValidation.isValid) {
          throw new Error(extValidation.error);
        }
    
        console.error(`Writing file: ${pathValidation.normalizedPath}`);
        // ディレクトリが存在しない場合は作成
        const dir = path.dirname(pathValidation.normalizedPath);
        await fs.mkdir(dir, { recursive: true });
    
        await fs.writeFile(pathValidation.normalizedPath, content, "utf-8");
        return {
          content: [
            {
              type: "text",
              text: `ファイル "${pathValidation.normalizedPath}" に正常に書き込みました`,
            },
          ],
          isError: false,
        };
      } catch (error) {
        throw new Error(`ファイルの書き込みに失敗: ${error}`);
      }
    }
  • src/index.ts:129-146 (registration)
    Registration of the 'write_file' tool in the TOOLS constant array. Includes the tool name, description, and inputSchema for parameter validation (filepath: string, content: string). This is returned by ListToolsRequest.
    {
      name: "write_file",
      description: "指定されたファイルにテキストを書き込みます(許可されたディレクトリのみ)",
      inputSchema: {
        type: "object",
        properties: {
          filepath: {
            type: "string",
            description: "書き込むファイルのパス",
          },
          content: {
            type: "string",
            description: "書き込む内容",
          },
        },
        required: ["filepath", "content"],
      },
    },
  • The dispatch case in the CallToolRequest handler switch statement that routes 'write_file' calls to the writeFile method, extracting filepath and content from arguments.
    case "write_file":
      return await this.writeFile(
        args.filepath as string,
        args.content as string
      );
  • The inputSchema definition for the 'write_file' tool, specifying required string parameters filepath and content with descriptions.
    inputSchema: {
      type: "object",
      properties: {
        filepath: {
          type: "string",
          description: "書き込むファイルのパス",
        },
        content: {
          type: "string",
          description: "書き込む内容",
        },
      },
      required: ["filepath", "content"],
    },
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden. It mentions the 'only permitted directories' restriction, which is useful behavioral context. However, it doesn't disclose whether this overwrites existing files, appends, requires specific permissions, handles encoding, or what happens on success/failure. For a file write operation with zero annotation coverage, this leaves significant behavioral gaps.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence in Japanese that communicates the core functionality. It's appropriately sized and front-loaded with the main action. No wasted words, though it could potentially benefit from slightly more detail given the lack of annotations.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a file write operation with no annotations and no output schema, the description is incomplete. It mentions directory restrictions but doesn't cover overwrite behavior, error handling, permissions, or return values. Given the complexity of file operations and the lack of structured metadata, more comprehensive guidance would be helpful.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents both parameters (filepath and content) adequately. The description doesn't add any parameter-specific details beyond what the schema provides. The baseline of 3 is appropriate when the schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('書き込みます' - writes) and resource ('ファイル' - file) with specific scope ('テキスト' - text, '許可されたディレクトリのみ' - only permitted directories). It distinguishes from sibling read_file (read vs write) but doesn't explicitly differentiate from create_sample_file which might also create/write files.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description mentions '許可されたディレクトリのみ' (only permitted directories), which provides some context about restrictions, but doesn't explicitly state when to use this tool versus alternatives like create_sample_file. No guidance on prerequisites, error conditions, or comparison with sibling tools.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other 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/p-united/mcpSample'

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