---
title: Code Execution with the MCP Server
description: >-
  Run agent-authored JavaScript against Datadog APIs in a single MCP tool call
  to investigate issues and modify supported resources.
breadcrumbs: Docs > Datadog MCP Server > Code Execution with the MCP Server
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Code Execution with the MCP Server

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

## Overview{% #overview %}

Code execution in the Datadog MCP Server lets your AI agent write and run JavaScript against Datadog APIs in a single MCP tool call, instead of one tool call per API request. The agent's generated code runs inside a Datadog-managed sandbox. The only data sent back to the agent is the value the code returns. This keeps large API responses out of the model's context.

Use code execution for investigations that span multiple Datadog products, or that need the agent to join, filter, or summarize data from several calls. An example is correlating error logs with APM latency for the same service and time window.

With `execute_code_write`, agents can also create, update, or delete supported Datadog resources and act on findings in the same workflow. For example, an agent can investigate an error spike, create a case, and verify the case details in one script.

## Why use code execution{% #why-use-code-execution %}

Without code execution, an agent instructed to enrich the top error-generating services with APM latency data needs a separate tool call for each service. It also needs additional turns to combine the results. Each of those calls and turns consumes context window space.

With code execution, the agent expresses the same investigation as a single script:

1. Query logs for the services with the most error logs in a time window.
1. For each returned service, query spans for latency data.
1. Join the two result sets and return a compact object.

The MCP Server runs the script and returns only the joined result. The agent completes the investigation in one tool call instead of one call per service.

## Available tools{% #available-tools %}

The `core` and `code-exec` toolsets provide:

| Tool                                                                                      | Description                                                                                                                |
| ----------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
| [`execute_code`](https://docs.datadoghq.com/mcp_server/tools.md#execute_code)             | Runs JavaScript with read-only API access in the sandbox for investigations and analysis, and returns a structured result. |
| [`execute_code_write`](https://docs.datadoghq.com/mcp_server/tools.md#execute_code_write) | Runs JavaScript in the sandbox to create, update, or delete supported Datadog resources, and returns a structured result.  |
| [`search_datadog_sdk`](https://docs.datadoghq.com/mcp_server/tools.md#search_datadog_sdk) | Looks up SDK operations, request types, and response types.                                                                |

See the linked entries in the MCP Server Tools reference for permissions and example prompts.

Generated code is JavaScript based on the public [Datadog API Client for TypeScript](https://github.com/DataDog/datadog-api-client-typescript).

## What the sandbox can access{% #what-the-sandbox-can-access %}

Code execution scripts run against Datadog APIs using your user identity. Every API call is authorized independently with your existing [role permissions](https://docs.datadoghq.com/account_management/rbac/permissions.md). Code execution can't grant access beyond those permissions.

- `execute_code` exposes read-only Datadog API calls for investigations and analysis.
- `execute_code_write` can create, update, or delete supported resources, such as monitors, SLOs, log configurations, incidents, and cases. Use `search_datadog_sdk` with `target: "execute_code_write"` to confirm which operations are available.
- Teams access is read-only. Creating or updating teams, changing membership, and other team write operations are unavailable through code execution.
- Dashboard and notebook creation and updates are unavailable through code execution. Use [`upsert_datadog_dashboard`](https://docs.datadoghq.com/mcp_server/tools.md#upsert_datadog_dashboard) and the dedicated [notebook tools](https://docs.datadoghq.com/mcp_server/tools.md#notebooks) for those actions. Code execution supports reading and deleting dashboards and notebooks.
- Credential management, user and role management, and other sensitive administrative APIs remain unavailable, even if you can access them elsewhere in Datadog.
- The sandbox is isolated. Scripts can't access your local machine, file system, arbitrary network destinations, or raw Datadog credentials.
- Raw API responses stay inside the sandbox while the script processes them. The only data sent back to the agent is the value the code returns. Review what a script returns if the underlying data is sensitive, such as customer data stored in logs.

## Enable code execution{% #enable-code-execution %}

The default `core` toolset includes all three code execution tools: `execute_code`, `execute_code_write`, and `search_datadog_sdk`.

To enable only code execution tools, set `toolsets=code-exec` when connecting your AI client to the Datadog MCP Server. See [Set Up the Datadog MCP Server](https://docs.datadoghq.com/mcp_server/setup.md) for client-specific connection instructions. Toolset selection does not change the required [role permissions](https://docs.datadoghq.com/account_management/rbac/permissions.md).

{% callout %}
# Important note for users on the following Datadog sites: app.datadoghq.com, us3.datadoghq.com, us5.datadoghq.com, app.datadoghq.eu, ap1.datadoghq.com, ap2.datadoghq.com, uk1.datadoghq.com



Based on your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md) ({% placeholder "user-datadog-site-name" /%}), this URL enables only code execution tools:

```
<YOUR_MCP_SERVER_ENDPOINT>?toolsets=code-exec
```

`toolsets=all` also includes all three code execution tools.


{% /callout %}

### Enable write operations{% #enable-write-operations %}

The `execute_code_write` tool is included in the `core` and `code-exec` toolsets and in `toolsets=all`.

To use write operations:

1. Ask an organization administrator to enable **MCP Write Access** in Organization Settings. When it's disabled, the MCP Server hides write tools from all users. See [Write tools are missing](https://docs.datadoghq.com/mcp_server/setup.md#write-tools-are-missing).
1. Assign your role the `mcp_write` permission and the permissions required for the resources being modified. For example, creating a monitor requires `Monitors Write` in addition to `mcp_write`.
1. If your AI client connected before your role had `mcp_write`, reauthorize the MCP connection to consent to the additional write scopes.

## Review and retry write operations safely{% #review-and-retry-write-operations-safely %}

{% alert level="warning" %}
`execute_code_write` can overwrite or delete data, and rerunning a script can duplicate changes. Scripts are not transactions. Completed writes remain if a later operation fails or times out, and a lost response can leave the result of a write unknown.
{% /alert %}

The AI client's normal approval behavior applies. The MCP Server does not add a separate confirmation step.

Before retrying a script:

1. Review the tool result, including partial-failure diagnostics.
1. Inspect the affected resources to determine which operations completed.
1. Retry only incomplete operations instead of rerunning the whole script.

For sensitive changes, ask the agent to show its plan before writing. Use narrowly scoped prompts, and request a compact result with affected resource IDs and completed actions.

## Example prompts{% #example-prompts %}

The following prompts use `execute_code_write` to change Datadog resources:

- Create a monitor for `checkout-api` that alerts when its error rate exceeds 5% over 5 minutes. Return the monitor ID and verify its query and notification message.
- Find monitors owned by `payments-team` that have not notified in 90 days. Show the proposed changes, then mute only approved monitors.
- Create a case for the current incident, assign it to the on-call team, and include links to the relevant traces and logs.

## Further reading{% #further-reading %}

Additional helpful documentation, links, and articles:

- [Datadog MCP Server](https://docs.datadoghq.com/mcp_server.md)
- [Set Up the Datadog MCP Server](https://docs.datadoghq.com/mcp_server/setup.md)
- [Datadog MCP Server Tools](https://docs.datadoghq.com/mcp_server/tools.md)
