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
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes |
Implementation Reference
- src/stata_mcp/mcp_servers.py:415-452 (handler)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)
- src/stata_mcp/mcp_servers.py:415-418 (registration)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" )