Skip to main content
Glama
alxspiker

AI Meta MCP Server

define_function

Create custom functions for AI models to execute in JavaScript, Python, or Shell environments with sandboxed security and approval workflows.

Instructions

Create a new custom MCP function that the AI can use

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
nameYesName of the new function
descriptionYesDescription of what the function does
parameters_schemaYesJSON Schema for parameters
implementation_codeYesCode to implement the function
execution_environmentNoEnvironment to execute the code injavascript

Implementation Reference

  • The core handler function for the 'define_function' tool. It validates inputs, checks for existing functions, creates a StoredTool object, registers it with the server using registerToolWithServer, stores it in customTools, and saves to database. Returns success or error response.
    async ({ name, description, parameters_schema, implementation_code, execution_environment }) => {
      console.error(`Defining new function: ${name}`);
      
      // Check if function already exists
      if (customTools[name]) {
        return {
          isError: true,
          content: [
            {
              type: "text",
              text: `A function named "${name}" already exists. Use update_function to modify it.`,
            },
          ],
        };
      }
    
      // Validate execution environment
      if (execution_environment === "javascript" && !ALLOW_JS_EXECUTION) {
        return {
          isError: true,
          content: [
            {
              type: "text",
              text: "JavaScript execution is not allowed in this environment.",
            },
          ],
        };
      }
      if (execution_environment === "python" && !ALLOW_PYTHON_EXECUTION) {
        return {
          isError: true,
          content: [
            {
              type: "text",
              text: "Python execution is not allowed in this environment.",
            },
          ],
        };
      }
      if (execution_environment === "shell" && !ALLOW_SHELL_EXECUTION) {
        return {
          isError: true,
          content: [
            {
              type: "text",
              text: "Shell execution is not allowed in this environment.",
            },
          ],
        };
      }
    
      // Create a new tool definition
      const now = new Date();
      const toolDef: StoredTool = {
        name,
        description,
        inputSchema: parameters_schema,
        implementation: implementation_code,
        executionEnvironment: execution_environment,
        createdAt: now,
        updatedAt: now,
      };
    
      // Register the tool
      try {
        registerToolWithServer(toolDef);
        
        // Store the tool
        customTools[name] = toolDef;
        await saveToolsDatabase();
    
        return {
          content: [
            {
              type: "text",
              text: `Successfully created new function "${name}". You can now use it as a tool.`,
            },
          ],
        };
      } catch (error) {
        return {
          isError: true,
          content: [
            {
              type: "text",
              text: `Error creating function: ${error instanceof Error ? error.message : String(error)}`,
            },
          ],
        };
      }
    }
  • Zod-based input schema defining the parameters for the define_function tool: name, description, parameters_schema (JSON schema), implementation_code, and execution_environment.
    {
      name: z.string().min(1).describe("Name of the new function"),
      description: z.string().describe("Description of what the function does"),
      parameters_schema: z.record(z.any()).describe("JSON Schema for parameters"),
      implementation_code: z.string().min(1).describe("Code to implement the function"),
      execution_environment: z.enum(["javascript", "python", "shell"]).default("javascript").describe("Environment to execute the code in"),
    },
  • src/index.ts:205-306 (registration)
    The server.tool() call that registers the 'define_function' tool with the MCP server, specifying name, description, input schema, and handler function.
    server.tool(
      "define_function",
      "Create a new custom MCP function that the AI can use",
      {
        name: z.string().min(1).describe("Name of the new function"),
        description: z.string().describe("Description of what the function does"),
        parameters_schema: z.record(z.any()).describe("JSON Schema for parameters"),
        implementation_code: z.string().min(1).describe("Code to implement the function"),
        execution_environment: z.enum(["javascript", "python", "shell"]).default("javascript").describe("Environment to execute the code in"),
      },
      async ({ name, description, parameters_schema, implementation_code, execution_environment }) => {
        console.error(`Defining new function: ${name}`);
        
        // Check if function already exists
        if (customTools[name]) {
          return {
            isError: true,
            content: [
              {
                type: "text",
                text: `A function named "${name}" already exists. Use update_function to modify it.`,
              },
            ],
          };
        }
    
        // Validate execution environment
        if (execution_environment === "javascript" && !ALLOW_JS_EXECUTION) {
          return {
            isError: true,
            content: [
              {
                type: "text",
                text: "JavaScript execution is not allowed in this environment.",
              },
            ],
          };
        }
        if (execution_environment === "python" && !ALLOW_PYTHON_EXECUTION) {
          return {
            isError: true,
            content: [
              {
                type: "text",
                text: "Python execution is not allowed in this environment.",
              },
            ],
          };
        }
        if (execution_environment === "shell" && !ALLOW_SHELL_EXECUTION) {
          return {
            isError: true,
            content: [
              {
                type: "text",
                text: "Shell execution is not allowed in this environment.",
              },
            ],
          };
        }
    
        // Create a new tool definition
        const now = new Date();
        const toolDef: StoredTool = {
          name,
          description,
          inputSchema: parameters_schema,
          implementation: implementation_code,
          executionEnvironment: execution_environment,
          createdAt: now,
          updatedAt: now,
        };
    
        // Register the tool
        try {
          registerToolWithServer(toolDef);
          
          // Store the tool
          customTools[name] = toolDef;
          await saveToolsDatabase();
    
          return {
            content: [
              {
                type: "text",
                text: `Successfully created new function "${name}". You can now use it as a tool.`,
              },
            ],
          };
        } catch (error) {
          return {
            isError: true,
            content: [
              {
                type: "text",
                text: `Error creating function: ${error instanceof Error ? error.message : String(error)}`,
              },
            ],
          };
        }
      }
    );
  • Helper function called by define_function handler to register the new tool with the MCP server. Handles schema parsing and creates the execution wrapper.
    function registerToolWithServer(toolDef: StoredTool) {
      try {
        let paramsSchema: z.ZodRawShape = {};
        
        // Handle both string schemas and ZodType objects
        if (typeof toolDef.inputSchema === "string") {
          try {
            paramsSchema = JSON.parse(toolDef.inputSchema as string);
          } catch (e) {
            console.error(`Failed to parse schema for tool ${toolDef.name}:`, e);
            // Keep empty object as default
          }
        } else if (toolDef.inputSchema instanceof z.ZodObject) {
          // If it's a ZodObject, extract its shape which is a ZodRawShape
          paramsSchema = toolDef.inputSchema.shape;
        } else if (typeof toolDef.inputSchema === "object" && toolDef.inputSchema !== null) {
          // Assume it's a raw object schema
          paramsSchema = toolDef.inputSchema as Record<string, any>;
        }
    
        // Register the tool with the server
        server.tool(
          toolDef.name,
          toolDef.description,
          paramsSchema,
          async (params) => {
            console.error(`Executing custom tool ${toolDef.name} with parameters:`, params);
            try {
              let result;
              switch (toolDef.executionEnvironment) {
                case "javascript":
                  result = await executeJavaScript(toolDef.implementation, params);
                  break;
                case "python":
                  result = await executePython(toolDef.implementation, params);
                  break;
                case "shell":
                  result = await executeShell(toolDef.implementation, params);
                  break;
                default:
                  throw new Error(`Unsupported execution environment: ${toolDef.executionEnvironment}`);
              }
    
              // Ensure we return a proper CallToolResult
              return {
                content: [
                  {
                    type: "text",
                    text: typeof result === "string" ? result : JSON.stringify(result, null, 2),
                  },
                ],
              };
            } catch (error) {
              console.error(`Error executing tool ${toolDef.name}:`, error);
              return {
                isError: true,
                content: [
                  {
                    type: "text",
                    text: `Error executing tool: ${error instanceof Error ? error.message : String(error)}`,
                  },
                ],
              };
            }
          }
        );
        console.error(`Registered custom tool: ${toolDef.name}`);
      } catch (error) {
        console.error(`Failed to register tool ${toolDef.name}:`, error);
      }
    }
Behavior2/5

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

With no annotations provided, the description carries full burden but only states the basic action without disclosing behavioral traits. It doesn't mention permissions needed, whether creation is idempotent, error handling, or side effects, which is inadequate for a mutation tool.

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's appropriately sized and front-loaded, making it easy to understand quickly.

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 creating a custom function with 5 parameters, no annotations, and no output schema, the description is insufficient. It lacks details on return values, error cases, or how the function integrates with the MCP system, leaving significant gaps.

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?

Schema description coverage is 100%, so the schema fully documents all 5 parameters. The description adds no additional meaning beyond what's in the schema, such as explaining parameter interactions or constraints, meeting the baseline for high coverage.

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 action ('Create') and resource ('new custom MCP function'), making the purpose evident. However, it doesn't differentiate from sibling tools like 'update_function' or specify that this is for initial creation only, which prevents a perfect score.

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?

No guidance is provided on when to use this tool versus alternatives like 'update_function' or 'delete_function'. The description lacks context about prerequisites, such as whether functions must be unique or if this overrides existing ones, leaving usage unclear.

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/alxspiker/ai-meta-mcp-server'

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