GUIDE

Turn a JavaScript function into an MCP tool

2 min read
mcp ai

A Microfn function can be a tool your AI assistant calls directly. Start with something small: a function that turns a title into a URL slug, like Hello World! into hello-world.

You'll need a Microfn account and an MCP client, such as Claude.

1. Create the function

Open Microfn, create a function named slugify, and paste this code into the editor:

export function main(input: string) {
  return input
    .trim()
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, "-")
    .replace(/^-+|-+$/g, "");
}

Click Deploy and wait for Saved. The function takes a string and returns a lowercase slug. This example keeps ASCII letters and numbers; it doesn't transliterate accented characters.

2. Expose it as a tool

Open Triggers and enable Direct MCP Tool. The change saves immediately, and Microfn lists the function as fn_slugify.

This lets your assistant call the function by name instead of going through the generic executeFunction tool.

3. Connect your assistant

Add a remote MCP server in your client's settings using this URL:

https://mcp.microfn.dev/mcp

Sign in to Microfn when prompted. For the exact steps in your client, see Connect Microfn via MCP.

If you've already connected Microfn, refresh the client's tool list or reconnect so it picks up fn_slugify.

4. Try it

Ask your assistant:

Use my slugify tool to turn "My First MCP Tool!" into a URL slug.

The tool call takes this input:

{"input": "My First MCP Tool!"}

And returns:

"my-first-mcp-tool"

Check that the assistant actually called fn_slugify, rather than producing the answer itself.

Use the same steps for your own functions

Replace the slug conversion with the work you want your assistant to do, deploy the function, and enable Direct MCP Tool. For a function that needs persistent data, see the key-value storage module.

All guides