Building Speakeasy Functions
Relevant source files
The following files were used as context for generating this wiki page:
Speakeasy Functions allow you to create tools for LLMs and Model Context Protocol (MCP) servers. These functions perform arbitrary tasks such as fetching data from external APIs, performing complex calculations, or interacting with hosted databases. You build these functions using a tiny TypeScript framework that integrates with the Speakeasy AI Control Plane.
Sources: cli/internal/functions/templates/functions/README.md, examples/snowflake/README.md
Core Framework Architecture
The framework relies on the Functions class as the central entry point for tool definition. You register multiple tools by chaining calls to the .tool() method. Each tool encapsulates specific logic, input validation, and metadata for discovery by LLMs.
The Functions Class
The Functions instance manages the lifecycle of tool registration and request handling. You must export the handleToolCall method from this instance to process incoming requests from the Speakeasy platform.
Sources: examples/pylon-basic/AGENTS.md:21-42
Tool Definition Structure
Each tool registration requires a configuration object defining its behavior:
| Property | Type | Description |
|---|---|---|
name | string | A unique identifier for the tool. |
description | string | (Optional) A human-readable description for the LLM. |
inputSchema | Zod Schema | Defines expected input parameters using Zod. |
execute | Function | Async implementation of the tool logic. |
Sources: examples/pylon-basic/AGENTS.md:44-50
Tool Execution Context
The execute function receives a ctx (context) object. This object provides helper methods for returning responses, managing environment variables, and handling request cancellation.
Response Helpers
The context provides standardized methods to return different data formats:
- ctx.json(data): Returns a JSON object to the caller.
- ctx.text(data): Returns a plain text string.
- ctx.html(data): Returns an HTML string.
- ctx.markdown(data): Returns a Markdown-formatted string.
Sources: examples/pylon-basic/AGENTS.md:52-85
Environment and Cancellation
- ctx.env: Accesses parsed environment variables validated against the
envSchema. - ctx.signal: Provides an
AbortSignalto handle request timeouts or user cancellations during long-running operations.
Sources: examples/pylon-basic/AGENTS.md:87-104
Data Flow and Validation
Speakeasy Functions use Zod for strict input validation and type safety. The framework ensures that the input received by the execute function matches the defined inputSchema.
Request Handling Flow
The following diagram illustrates the sequence of events when a tool is called:
This diagram shows the validation gate and execution path for a single tool call.
Sources: examples/pylon-basic/AGENTS.md:106-120, examples/pylon-basic/AGENTS.md:230-250
Configuration and Environment
Environment variables required by tools must be defined during framework instantiation. This registration allows the Speakeasy platform to prompt users for configuration values during deployment.
Defining Variables
You define variables using a Zod object schema. You can also provide default values for local development in the env property.
const functions = new Functions({
envSchema: {
API_KEY: z.string().describe("API key for external service"),
BASE_URL: z.string().check(z.url()).describe("Base URL for API requests"),
},
env: {
API_KEY: "local-dev-key"
}
});
Sources: examples/pylon-basic/AGENTS.md:126-155
Error Handling
The framework provides the ctx.fail() and assert() utilities to manage operational errors.
Using assert()
The assert function validates conditions and immediately halts execution if the condition is false. It throws a Response object that the framework catches and returns to the client.
| Parameter | Type | Description |
|---|---|---|
condition | any | The expression to evaluate. |
data | Object | Error data (must include an error field). |
options | Object | Optional settings like { status: number }. |
Sources: examples/pylon-basic/AGENTS.md:200-222
Deployment Workflow
Speakeasy Functions are managed via the Speakeasy CLI. The workflow involves building a deployment package and pushing it to the Speakeasy AI Control Plane.
CLI Commands
The following table summarizes the primary development and deployment commands:
| Command | Purpose |
|---|---|
speakeasy functions build | Bundles code into a zip file in the dist/ directory. |
speakeasy functions push | Deploys the built function to the Speakeasy cloud. |
speakeasy functions dev | Starts a local MCP server with the MCP Inspector. |
Sources: cli/internal/functions/templates/functions/README.md:26-47, examples/functions-basic/README.md:32-52
Development Cycle Flow
This flowchart represents the standard path from local development to cloud deployment.
Sources: examples/pylon-basic/AGENTS.md:10-18, cli/internal/functions/templates/functions/README.md:31-41
Conclusion
Building Speakeasy Functions provides a type-safe, structured approach to extending LLM capabilities. By utilizing the TypeScript framework and the Speakeasy CLI, you can rapidly develop, test locally via the MCP Inspector, and deploy robust tools that integrate external data and services into the AI Control Plane.