cfg_preprocess
Preprocess OpenSIPS configuration files by running m4 on opensips.cfg.m4 and local.cfg.m4, then optionally validate the output with opensips -C -f.
Instructions
Run m4 local.cfg.m4 opensips.cfg.m4 and optionally validate the output.
Parameters
main_m4:
Contents of opensips.cfg.m4.
local_m4:
Contents of local.cfg.m4 (site-specific define(...) lines).
validate:
If True and the opensips binary is present, run opensips -C -f
on the preprocessed output.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| main_m4 | Yes | ||
| local_m4 | Yes | ||
| validate | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- The cfg_preprocess tool handler - runs m4 preprocessing on m4 config files and optionally validates the output with opensips -C -f.
async def cfg_preprocess( ctx: Context, main_m4: str, local_m4: str, validate: bool = True, ) -> dict[str, Any]: """Run ``m4 local.cfg.m4 opensips.cfg.m4`` and optionally validate the output. Parameters ---------- main_m4: Contents of ``opensips.cfg.m4``. local_m4: Contents of ``local.cfg.m4`` (site-specific ``define(...)`` lines). validate: If True and the ``opensips`` binary is present, run ``opensips -C -f`` on the preprocessed output. """ pre = await run_m4(main_m4, local_m4) payload: dict[str, Any] = { "output_cfg": pre.output_cfg, "m4_stderr": pre.m4_stderr, "m4_returncode": pre.m4_returncode, "m4_success": pre.success, } if validate and pre.success: payload["validation"] = await _validate_if_available(pre.output_cfg) elif validate: payload["validation"] = { "valid": False, "errors": ["m4 preprocessing failed; skipping opensips validation."], "warnings": [], "raw_output": "", } return payload - src/opensips_mcp/tools/cfg_tools.py:806-811 (registration)The tool is registered via the @mcp.tool() decorator at line 804, making it available as an MCP tool named 'cfg_preprocess'.
async def cfg_preprocess( ctx: Context, main_m4: str, local_m4: str, validate: bool = True, ) -> dict[str, Any]: - The _validate_if_available helper used by cfg_preprocess to optionally validate the preprocessed output via opensips -C -f.
async def _validate_if_available(cfg_text: str) -> dict[str, Any]: """Run ``opensips -C -f`` when the binary exists; return a dict payload.""" try: result = await _validator.validate(cfg_text) return result.model_dump() except FileNotFoundError: return { "valid": None, "errors": [], "warnings": [], "raw_output": "", "note": "opensips binary not found — validation skipped.", } except Exception as exc: return { "valid": False, "errors": [str(exc)], "warnings": [], "raw_output": "", } - src/opensips_mcp/tools/cfg_tools.py:18-23 (registration)The run_m4 helper import from cfg.m4_builder, which is the core preprocessing function called by cfg_preprocess.
M4ConfigBuilder, check_m4_binary, is_safe_out_dir, run_m4, ) from opensips_mcp.cfg.migration import ConfigMigrator