Skip to main content
Glama

write_dofile

Generate Stata code in a dofile format for regression analysis and statistical operations with structured inputs using the Stata-MCP server.

Instructions

write the stata-code to dofile

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contentYes

Implementation Reference

  • The handler function decorated with the tool registration for write_dofile. It writes the provided Stata code content to a new timestamped .do file in the dofile_base_path and returns the path to the created file.
    @stata_mcp.tool(
        name="write_dofile",
        description="write the stata-code to dofile"
    )
    def write_dofile(content: str, encoding: str = None, strict_mode: bool = False) -> str:
        """
        Write stata code to a dofile and return the do-file path.
    
        Args:
            content (str): The stata code content which will be writen to the designated do-file.
            encoding (str): The encoding method for the dofile, default -> 'utf-8'
    
        Returns:
            the do-file path
    
        Notes:
            Please be careful about the first command in dofile should be use data.
            For avoiding make mistake, you can generate stata-code with the function from `StataCommandGenerator` class.
            Please avoid writing any code that draws graphics or requires human intervention for uncertainty bug.
            If you find something went wrong about the code, you can use the function from `StataCommandGenerator` class.
    
        Enhancement:
            If you have `outreg2`, `esttab` command for output the result,
            you should use the follow command to get the output path.
            `results_doc_path`, and use `local output_path path` the path is the return of the function `results_doc_path`.
            If you want to use the function `write_dofile`, please use `results_doc_path` before which is necessary.
    
        """
        file_path = dofile_base_path / f"{datetime.strftime(datetime.now(), '%Y%m%d%H%M%S')}.do"
        encoding = encoding or "utf-8"
        try:
            with open(file_path, "w", encoding=encoding) as f:
                f.write(content)
            logging.info(f"Successful write dofile to {file_path}")
        except Exception as e:
            logging.error(f"Failed to write dofile to {file_path}: {str(e)}")
        return str(file_path)
  • The @stata_mcp.tool decorator registers the write_dofile function as an MCP tool.
    @stata_mcp.tool(
        name="write_dofile",
        description="write the stata-code to dofile"
    )

Tool Definition Quality

Score is being calculated. Check back soon.

Install Server

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/SepineTam/stata-mcp'

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