{"rules":[{"when":"user asks to create a launch file","then":"always use the choose_template prompt"},{"when":"user mentions gazebo, bridge, or rviz","then":"map to the generate_file tool with the corresponding template"}]}
importloggingfrompathlibimportPathfrommcp.server.fastmcpimportFastMCP# --- Setup ---logging.basicConfig(level=logging.INFO,format="%(asctime)s [%(levelname)s] %(message)s")log=logging.getLogger("template-mcp")mcp=FastMCP("TemplateBuilder")log.info("MCP server initialized. -----------")TEMPLATES_DIR=Path("templates")# -------------------------------------------------# TOOL → Generate file from a template# -------------------------------------------------@mcp.tool()defgenerate_file(template:str,output_name:str,**kwargs)->dict:""" Generate a new file from a given template. Args: template: filename in templates/ (e.g. gazebo.launch.py) output_name: new file name (e.g. my_gazebo.launch.py) kwargs: placeholder values to substitute """template_path=TEMPLATES_DIR/templateifnottemplate_path.exists():raiseValueError(f"Template '{template}' not found in {TEMPLATES_DIR}")content=template_path.read_text()# Replace placeholders like {{robot_name}} with values from kwargsforkey,valueinkwargs.items():content=content.replace(f"{{{{{key}}}}}",str(value))log.info(f"Generated {output_name} from {template}")return{"filename":output_name,"content":content}# -------------------------------------------------# PROMPT → Choose the right template# -------------------------------------------------@mcp.prompt()defchoose_template(subject:str)->str:""" Guide the AI to pick the right template based on the subject. """available=[f.nameforfinTEMPLATES_DIR.glob("*.launch.py")]prompt=f""" The user wants: "{subject}" Available templates:{available} Select the best template file name and call generate_file with: - template: <chosen template> - output_name: a descriptive filename for the project - kwargs: any substitutions needed (e.g. world="my_world.sdf") """log.info(f"Prompting AI with subject: {subject}")log.info(available)returnprompt# -------------------------------------------------# Entrypoint# -------------------------------------------------if__name__=="__main__":log.info("Starting TemplateBuilder MCP server...")mcp.run()