Svelte AI Tools
repository·main·Indexed 43 days ago
https://github.com/sveltejs/ai-toolsA suite of tools for AI agents to write robust Svelte code, featuring an official Model Context Protocol (MCP) server. It provides tools like list-sections, get-documentation, svelte-autofixer, and playground-link to allow agents to interact with Svelte 5 and SvelteKit documentation and perform code analysis. The repository also includes @sveltejs/opencode for plugin management and tmcp, a schema-agnostic MCP server implementation supporting adapters for Zod, Valibot, ArkType, and Effect Schema.
What's inside Svelte AI Tools
- The Svelte MCP (Model Context Protocol) server is designed to improve the quality of Svelte code generated by AI agents. It achieves this by providing the agent with relevant Svelte documentation and performing static analysis on generated code to suggest fixes and enforce best practices.
Overview of Svelte AI tools
mainThe Svelte team maintains a suite of four AI tools designed to help AI agents write correct and robust Svelte code. While they are designed to work together, they can be used individually depending on your needs:
- Instructions: Small prompts injected into your session to increase agent awareness of the available tools.
- MCP Server: Provides tools, prompts, and resources to give agents context by pulling directly from official Svelte documentation and using static analysis to correct common generative AI errors.
- Skills: Lazy-loaded descriptions that teach agents Svelte best practices and how to use the
@sveltejs/mcpCLI. - Subagents: Focused agents that can be invoked in parallel to execute atomic operations in a separate context window.
Available MCP tools for documentation and code fixing
mainThe MCP server provides several tools that an LLM can invoke to interact with Svelte documentation and improve generated code. These tools include:
list-sections: Retrieves a list of all available documentation sections.get-documentation: Fetches the full, up-to-date documentation for specific sections directly fromsvelte.dev/docs.svelte-autofixer: Performs static analysis on LLM-generated code to provide suggestions. This tool is designed to be used in an agentic loop where the model repeatedly calls it until all issues are resolved.playground-link: Creates an ephemeral link containing the generated code, allowing for quick testing of solutions that haven't been written to a local file. Note that the code is stored within the URL itself, resulting in large URLs.
What is the Svelte File Editor Subagent?
mainThesvelte-file-editoris a specialized subagent automatically activated when the agent creates, edits, or reviews.svelte,.svelte.ts, or.svelte.jsfiles. It uses the Svelte MCP server tools to fetch documentation and validate code during the editing process.What is the Svelte MCP Server?
mainThe Svelte MCP server provides tools that allow an AI agent to interact with Svelte documentation and code analysis. It exposes the following tools:
list-sections: Discover available Svelte 5 and SvelteKit documentation sections.get-documentation: Retrieve full documentation content for specific sections.svelte-autofixer: Analyze Svelte code to identify issues and receive suggestions.playground-link: Generate Svelte Playground links for provided code snippets.
Handle `$props` reactively
mainTreat props as values that will change over time. Any logic that depends on a prop should use
$derivedto ensure the dependent value updates when the prop changes.let { type } = $props(); // Correct: color updates when type changes let color = $derived(type === 'danger' ? 'red' : 'green'); // Incorrect: color will not update if type changes let color = type === 'danger' ? 'red' : 'green';What are Svelte Attachments and how do they work?
mainAttachments are functions that run within a Svelte effect when an element is mounted to the DOM or when reactive state read inside the function updates.
An attachment can optionally return a cleanup function. This returned function is called either before the attachment re-runs (due to state changes) or after the element is removed from the DOM.
Note: Attachments require Svelte 5.29 or newer.
<script> /** @type {import('svelte/attachments').Attachment} */ function myAttachment(element) { console.log(element.nodeName); // 'DIV' return () => { console.log('cleaning up'); }; } </script> <div {@attach myAttachment}>...</div>Serialize complex data and Promises with `hydratable`
mainData returned from a
hydratablefunction must be serializable. Svelte usesdevaluefor serialization, which supports more than just JSON, includingMap,Set,URL, andBigInt. Additionally, Svelte supports serializingPromiseobjects directly.When a
hydratablefunction returns a promise, the client canawaitthose promises just like standard asynchronous operations.<script> import { hydratable } from 'svelte'; const promises = hydratable('random', () => { return { one: Promise.resolve(1), two: Promise.resolve(2) } }); </script> {await promises.one} {await promises.two}Snippets vs Slots
mainIn Svelte 4, content was passed viaslots. In Svelte 5, slots are deprecated in favor of snippets, which offer greater flexibility and power.What the Svelte Cursor plugin provides
mainThe Svelte plugin for Cursor bundles several specialized tools to improve AI development with Svelte:
- Svelte MCP server: A local stdio server defined via the plugin's
.mcp.json. - Cursor skills: Specialized capabilities that appear in Cursor's rules UI.
- Always-on rule: A system rule that instructs the model on how to use Svelte MCP tools correctly.
svelte-file-editorsubagent: A dedicated agent available in chat specifically for working on.sveltefiles and.svelte.ts/.svelte.jsmodules.
- Svelte MCP server: A local stdio server defined via the plugin's
What are Attachments in Svelte
mainAttachments are functions that run within a Svelte effect when an element is mounted to the DOM or when reactive state read inside the function updates.
An attachment function receives the element as its argument. Optionally, it can return a cleanup function that is called before the attachment re-runs or when the element is removed from the DOM.
Note: Attachments are available in Svelte 5.29 and newer.
<script> /** @type {import('svelte/attachments').Attachment} */ function myAttachment(element) { console.log(element.nodeName); // 'DIV' return () => { console.log('cleaning up'); }; } </script> <div {@attach myAttachment}>...</div>Use `$effect` sparingly as an escape hatch
mainEffects should be avoided for most logic. Avoid updating state inside an effect.
When to use alternatives instead of
$effect:- Syncing to external libraries (e.g., D3): Use
{@attach ...}. - User interaction logic: Put code directly in event handlers or use function bindings.
- Debugging/Logging: Use
$inspect. - Observing external systems: Use
createSubscriber.
Important: Effects do not run on the server, so never wrap their contents in
if (browser)checks.- Syncing to external libraries (e.g., D3): Use