Skip to main content

Overview

Use the Code MCP server from your ATXP-powered agent to run code in a variety of languages in a sandboxed environment.

Example prompts

  • “Run the following python code: print(‘Hello, world!’)”
  • “What is the result of this javascript code: console.log(‘Hello, world!’);”
  • “What is the result of this Scheme code: (display ‘Hello, world!)”

Tools

Take in code and execute it in a sandbox. The output will be the result of the code execution. An example of how it can be used would be to run code generated by a LLM in a safe environment.

Arguments

Accepts a JSON object with the following properties:
code
string
required
The code to execute. It will be executed in a sandbox.
language
string
The programming language to use for execution (e.g., javascript, python, typescript, etc.). Default is typescript.

Response

Returns a JSON object with the following properties:
status
string
The status of the code execution. Returns “success” when the code is executed successfully.
output
string
The output of the code execution.
exitCode
number
The exit code of the code execution.

Usage

1

Define the Code service

Create a reusable service configuration that points to the MCP server and standardizes how you pass arguments and read results. This lets your agent easily interact with the Code tools in a consistent manner.
const codeService = {
    mcpServer: 'https://code.mcp.atxp.ai/',
    executeCodeToolName: 'code_execute_code',
    description: 'ATXP Code MCP server',
    getArguments: (code: string, language: string) => ({ code, language }),
    getResult: (result: any) => {
      const jsonResult = result.content[0].text
      return JSON.parse(jsonResult);
    }
  };
2

Create an ATXP client

  • Using an ATXP account
  • Using a Base account
  • Using a Solana account
  • Using a Worldchain account
  • Using a Polygon account
Create a client using an ATXP account by importing the ATXP client SDK and other dependencies.
// Import the ATXP client SDK
import { atxpClient, ATXPAccount } from '@atxp/client';

// Read the ATXP account details from environment variables
const atxpConnectionString = process.env.ATXP_CONNECTION;

// Create a client using the `atxpClient` function
const client = await atxpClient({
  mcpServer: codeService.mcpServer,
  account: new ATXPAccount(atxpConnectionString),
});
3

Use the Code service in your agent

Call the Code tool by passing your natural‑language instruction as the argument the getArguments method.Read the response using the getResult method.
const code = "print('Hello, world!')";
const language = "python";

try {
  const result = await client.callTool({
      name: codeService.executeCodeToolName,
      arguments: codeService.getArguments(code, language),
  });
  const result = codeService.getResult(result);
  console.log('Status:', result.status);
  console.log('Output:', result.output);
  console.log('Exit code:', result.exitCode);
} catch (error) {
  console.error(`Error with ${codeService.description}:`, error);
  process.exit(1);
}
You should see the result of the code printed in your console.
I