create-function
Generate edge functions for Deno runtime by writing code to files for version control. Create serverless functions that handle HTTP requests and return responses.
Instructions
Create a new edge function that runs in Deno runtime. The code must be written to a file first for version control
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| codeFile | Yes | Path to JavaScript file containing the function code. Must export: module.exports = async function(request) { return new Response(...) } | |
| description | No | ||
| name | Yes | ||
| slug | No | ||
| status | No | active |
Implementation Reference
- src/shared/tools.ts:812-875 (registration)Full registration of the 'create-function' MCP tool using server.tool(), including name, description, input schema, and handler function wrapped with usage tracking.server.tool( 'create-function', 'Create a new edge function that runs in Deno runtime. The code must be written to a file first for version control', { ...functionUploadRequestSchema.omit({ code: true }).shape, codeFile: z .string() .describe( 'Path to JavaScript file containing the function code. Must export: module.exports = async function(request) { return new Response(...) }' ), }, withUsageTracking('create-function', async (args) => { try { let code: string; try { code = await fs.readFile(args.codeFile, 'utf-8'); } catch (fileError) { throw new Error( `Failed to read code file '${args.codeFile}': ${fileError instanceof Error ? fileError.message : 'Unknown error'}` ); } const response = await fetch(`${API_BASE_URL}/api/functions`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-api-key': getApiKey(), }, body: JSON.stringify({ slug: args.slug, name: args.name, code: code, description: args.description, status: args.status, }), }); const result = await handleApiResponse(response); return await addBackgroundContext({ content: [ { type: 'text', text: formatSuccessMessage( `Edge function '${args.slug}' created successfully from ${args.codeFile}`, result ), }, ], }); } catch (error) { const errMsg = error instanceof Error ? error.message : 'Unknown error occurred'; return { content: [ { type: 'text', text: `Error creating function: ${errMsg}`, }, ], isError: true, }; } }) );
- src/shared/tools.ts:823-873 (handler)The handler function executes the tool: reads the function code from the provided file path, sends a POST request to the backend API to create the edge function, handles the response, and formats the output with success/error messages.withUsageTracking('create-function', async (args) => { try { let code: string; try { code = await fs.readFile(args.codeFile, 'utf-8'); } catch (fileError) { throw new Error( `Failed to read code file '${args.codeFile}': ${fileError instanceof Error ? fileError.message : 'Unknown error'}` ); } const response = await fetch(`${API_BASE_URL}/api/functions`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-api-key': getApiKey(), }, body: JSON.stringify({ slug: args.slug, name: args.name, code: code, description: args.description, status: args.status, }), }); const result = await handleApiResponse(response); return await addBackgroundContext({ content: [ { type: 'text', text: formatSuccessMessage( `Edge function '${args.slug}' created successfully from ${args.codeFile}`, result ), }, ], }); } catch (error) { const errMsg = error instanceof Error ? error.message : 'Unknown error occurred'; return { content: [ { type: 'text', text: `Error creating function: ${errMsg}`, }, ], isError: true, }; }
- src/shared/tools.ts:815-822 (schema)Input schema for 'create-function' tool: uses functionUploadRequestSchema (omitting 'code') and adds required 'codeFile' parameter for the path to the JS source file.{ ...functionUploadRequestSchema.omit({ code: true }).shape, codeFile: z .string() .describe( 'Path to JavaScript file containing the function code. Must export: module.exports = async function(request) { return new Response(...) }' ), },