Skip to main content
Glama
tofunori

Claude MCP Data Explorer

by tofunori

run-script

Execute JavaScript scripts to analyze and visualize data within Claude's Data Explorer for generating insights from loaded datasets.

Instructions

Execute a JavaScript script for data analysis and visualization

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYesJavaScript script to execute

Implementation Reference

  • Main handler function for the 'run-script' MCP tool in Python implementation. Dispatches to execute_script and handles errors.
    async def handle_run_script(arguments):
        """Handle the run-script tool"""
        script = arguments.get("script")
        
        if not script:
            return [TextContent(type="text", text="Error: script is required")]
        
        try:
            # Execute the script with access to loaded dataframes
            result = await execute_script(script)
            return [TextContent(type="text", text=result)]
        
        except Exception as e:
            error_message = f"Error executing script: {str(e)}\n{traceback.format_exc()}"
            logging.error(error_message)
            return [TextContent(
                type="text", 
                text=f"Error executing script: {str(e)}\n{traceback.format_exc()}"
            )]
  • Main handler function for the 'run-script' MCP tool in TypeScript implementation. Safely executes JavaScript code with access to loaded dataframes, capturing console output.
    export async function runScript(args: RunScriptArgs): Promise<{ type: string, text: string }[]> {
      const { script } = args;
      
      if (!script) {
        return [{ type: 'text', text: 'Error: script is required' }];
      }
      
      // Capture console output
      let consoleOutput: string[] = [];
      const originalConsoleLog = console.log;
      const originalConsoleError = console.error;
      const originalConsoleWarn = console.warn;
      
      console.log = (...args) => {
        consoleOutput.push(args.map(formatOutput).join(' '));
      };
      
      console.error = (...args) => {
        consoleOutput.push(`ERROR: ${args.map(formatOutput).join(' ')}`);
      };
      
      console.warn = (...args) => {
        consoleOutput.push(`WARNING: ${args.map(formatOutput).join(' ')}`);
      };
      
      try {
        // Create a context with available libraries and data
        const contextObject: Record<string, any> = {
          // Make loaded data frames available to the script
          ...getAllDataFrames(),
          // Add utilities
          require: (moduleName: string) => {
            try {
              // Only allow specific modules for security
              const allowedModules: { [key: string]: any } = {
                'simple-statistics': require('simple-statistics'),
                'papaparse': require('papaparse'),
              };
              
              if (moduleName in allowedModules) {
                return allowedModules[moduleName];
              } else {
                throw new Error(`Module not allowed: ${moduleName}`);
              }
            } catch (error) {
              throw new Error(`Error requiring module '${moduleName}': ${error}`);
            }
          },
          // Add global variables and functions
          console: {
            log: console.log,
            error: console.error,
            warn: console.warn
          },
          Math,
          Date,
          JSON,
          Object,
          Array,
          String,
          Number,
          Boolean,
          Map,
          Set,
          Promise,
          Error,
        };
        
        // Add Data Frame helper methods
        for (const [name, data] of Object.entries(getAllDataFrames())) {
          contextObject[name] = data;
          
          // Add common DataFrame operations
          if (Array.isArray(data) && data.length > 0) {
            // Use method binding to ensure 'this' is preserved
            contextObject[`${name}_describe`] = () => describeDataFrame(data);
            contextObject[`${name}_columns`] = () => Object.keys(data[0] || {});
            contextObject[`${name}_head`] = (n = 5) => data.slice(0, n);
            contextObject[`${name}_tail`] = (n = 5) => data.slice(-n);
            contextObject[`${name}_filter`] = (fn: (row: any) => boolean) => data.filter(fn);
            contextObject[`${name}_map`] = (fn: (row: any) => any) => data.map(fn);
            contextObject[`${name}_groupBy`] = (key: string) => {
              const groups: Record<string, any[]> = {};
              for (const row of data) {
                const groupKey = String(row[key]);
                if (!groups[groupKey]) {
                  groups[groupKey] = [];
                }
                groups[groupKey].push(row);
              }
              return groups;
            };
          }
        }
        
        // Create a secure function for execution
        const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
        const secureFunction = new AsyncFunction(
          ...Object.keys(contextObject),
          `"use strict";
          try {
            return (async () => {
              ${script}
              return "Script executed successfully";
            })();
          } catch (error) {
            throw error;
          }`
        );
        
        // Execute the function with context
        const result = await secureFunction(...Object.values(contextObject));
        
        // Add result to console output if we got something back
        if (result !== "Script executed successfully") {
          consoleOutput.push(formatOutput(result));
        }
        
        // Clean up and return the result
        return [{ type: 'text', text: consoleOutput.join('\n') }];
      } catch (error: any) {
        return [{ type: 'text', text: `Error executing script: ${error.message}\n\nConsole output:\n${consoleOutput.join('\n')}` }];
      } finally {
        // Restore original console methods
        console.log = originalConsoleLog;
        console.error = originalConsoleError;
        console.warn = originalConsoleWarn;
      }
    }
  • Input schema definition for the 'run-script' tool in the Python server's list_tools implementation.
    types.Tool(
        name="run-script",
        description="Execute a Python script for data analysis and visualization",
        inputSchema={
            "type": "object",
            "properties": {
                "script": {
                    "type": "string",
                    "description": "Python script to execute"
                }
            },
            "required": ["script"]
        }
    )
  • Input schema definition for the 'run-script' tool in the TypeScript server's list tools handler.
      name: "run-script",
      description: "Execute a JavaScript script for data analysis and visualization",
      inputSchema: {
        type: "object",
        properties: {
          script: {
            type: "string",
            description: "JavaScript script to execute"
          }
        },
        required: ["script"]
      }
    }]
  • Registration dispatch in the call_tool handler for the 'run-script' tool in Python server.
    elif name == "run-script":
        return await handle_run_script(arguments)
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the tool executes a JavaScript script but doesn't describe safety aspects (e.g., sandboxing, permissions), performance traits (e.g., execution time limits, resource usage), or what happens upon execution (e.g., output format, side effects). For a tool that runs arbitrary code with no annotation coverage, this is a significant gap.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that directly states the tool's purpose without unnecessary words. It is appropriately sized and front-loaded, with every part contributing essential information (verb, resource, domain).

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of executing arbitrary JavaScript code, the lack of annotations, and no output schema, the description is incomplete. It doesn't address critical aspects like security implications, error handling, or what the tool returns (e.g., visualization output, analysis results). For a tool with such potential impact, more context is needed.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 100% description coverage, with one parameter 'script' fully documented in the schema. The description adds no additional meaning about parameters beyond what the schema provides (e.g., no examples of script content, no constraints on JavaScript features). With high schema coverage, the baseline score of 3 is appropriate as the description doesn't compensate but doesn't detract either.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose with a specific verb ('Execute') and resource ('JavaScript script'), and specifies the domain ('for data analysis and visualization'). It doesn't distinguish from the sibling tool 'load-csv', which appears to be a different operation, so it doesn't explicitly differentiate from siblings.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It mentions the domain (data analysis and visualization) but doesn't specify prerequisites, limitations, or when not to use it. There's no explicit comparison with the sibling tool 'load-csv' or other potential tools.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/tofunori/claude-mcp-data-explorer'

If you have feedback or need assistance with the MCP directory API, please join our Discord server