"""
MCP Tools for Odoo Shell
=========================
MCP tool functions that provide access to Odoo shell functionality.
"""
from .server import get_shell_manager, mcp
@mcp.tool()
def execute_odoo_code(code: str) -> str:
"""
Execute Python code in an Odoo shell environment with full access to Odoo models and environment.
This tool provides direct access to the Odoo ORM and all loaded modules through
the shell environment. The ``env`` variable is available for accessing models,
and all standard Odoo shell features are accessible.
:param code: Python code to execute in the Odoo context
:type code: str
:return: The output from executing the code
:rtype: str
.. note::
The shell maintains persistent state between calls, so variables
defined in one execution will be available in subsequent calls.
.. warning::
Code executed through this tool has full access to the Odoo database
and can modify data. Use with appropriate caution.
"""
try:
shell = get_shell_manager()
result = shell.execute_code(code)
return result
except Exception as e:
return f"Error executing Odoo code: {str(e)}"
@mcp.tool()
def reset_odoo_shell() -> str:
"""
Reset the Odoo shell session (restart the shell process).
Terminates the current shell process and clears the global shell manager,
which will cause a new shell to be started on the next code execution.
This is useful for clearing session state or recovering from errors.
:return: Success message or error description
:rtype: str
"""
from .server import reset_shell_manager
try:
result = reset_shell_manager()
return result
except Exception as e:
return f"Error resetting shell: {str(e)}"
@mcp.tool()
def list_odoo_models(pattern: str = "") -> str:
"""
List available Odoo models, optionally filtered by pattern.
Queries the Odoo registry to retrieve all available model names.
If a pattern is provided, only models containing that pattern
in their name will be returned.
:param pattern: Optional pattern to filter model names (case-sensitive)
:type pattern: str
:return: List of model names, one per line
:rtype: str
.. note::
Results are limited to the first 50 matches to prevent overwhelming output.
"""
code = f"""
models = env.registry.keys()
if '{pattern}':
models = [m for m in models if '{pattern}' in m]
for model in sorted(models)[:50]: # Limit to first 50
print(model)
"""
return execute_odoo_code(code)
@mcp.tool()
def odoo_model_info(model_name: str) -> str:
"""
Get information about a specific Odoo model.
Retrieves detailed information about the specified model including
its description, database table name, and field definitions.
:param model_name: Name of the Odoo model (e.g., 'res.partner', 'sale.order')
:type model_name: str
:return: Formatted information about the model and its fields
:rtype: str
.. example::
>>> odoo_model_info('res.partner')
Model: res.partner
Description: Partner
Table: res_partner
Fields:
name: Char
email: Char
phone: Char
...
"""
code = f"""
try:
model = env['{model_name}']
print(f"Model: {model_name}")
print(f"Description: {{model._description}}")
print(f"Table: {{model._table}}")
print("\\nFields:")
for field_name, field in model._fields.items():
print(f" {{field_name}}: {{type(field).__name__}}")
except KeyError:
print(f"Model '{model_name}' not found")
except Exception as e:
print(f"Error: {{e}}")
"""
return execute_odoo_code(code)