---
title: Code Execution with the MCP Server
description: >-
  Run agent-authored JavaScript against Datadog APIs in a single MCP tool call
  to investigate issues that span multiple Datadog products.
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 %}

The `code-exec` toolset 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.

## 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 `code-exec` toolset provides:

- **`execute_code`**: Runs agent-authored JavaScript in the sandbox and returns a structured result. See [`execute_code`](https://docs.datadoghq.com/mcp_server/tools.md#execute_code) in the MCP Server Tools reference for permissions and example prompts.
- **`search_datadog_sdk`**: Looks up the SDK functions and API methods available to the agent for writing scripts. See [`search_datadog_sdk`](https://docs.datadoghq.com/mcp_server/tools.md#search_datadog_sdk) in the MCP Server Tools reference.

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 executed by the `code-exec` toolset runs against Datadog APIs using your user identity. An agent can only read data that you have permission to access. Other access limitations include:

- The sandbox is isolated. Scripts can't access your local machine, file system, arbitrary network destinations, or raw Datadog credentials.
- The sandbox only exposes read-only Datadog API calls. An agent can't use `execute_code` to perform write actions, such as creating a monitor or updating a dashboard.
- API calls made from a script apply your existing [role permissions](https://docs.datadoghq.com/account_management/rbac/permissions.md). If you don't have access to a dataset, the agent can't query it through `execute_code` either.
- 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 %}

To enable code execution, include `code-exec` in the `toolsets` query parameter when you connect 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.

{% 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



For example, based on your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md) ({% placeholder "user-datadog-site-name" /%}), this URL enables the core toolset alongside code execution:

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

`code-exec` is included in `toolsets=all`, so you don't need to add it separately if you already enable all generally available toolsets.


{% /callout %}

## 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)
