assist_with_cairo
Analyze, write, and refactor Cairo code and Starknet smart contracts using AI-powered assistance. Understand Starknet's ecosystem and implement best practices for efficient development.
Instructions
Provides assistance with Cairo and Starknet development tasks through AI-powered analysis.
Call this tool when the user's request involves writing, refactoring, implementing from scratch, or completing specific parts (like TODOs) of Cairo code or smart contracts.
The tool analyzes the query and context against Cairo/Starknet best practices and documentation, returning helpful information to generate accurate code or explanations.
This tool should also be called to get a better understanding of Starknet's ecosystem, features, and capacities.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| codeSnippets | No | Optional: Code snippets for context. This will help the tool understand the user's intent and provide more accurate answers. Provide as much relevant code as possible to fit the user's request. | |
| history | No | Optional: The preceding conversation history. This can help the tool understand the context of the discussion and provide more accurate answers. | |
| query | Yes | The user's question regarding Cairo and Starknet development. Try to be as specific as possible for better results (e.g., 'Using OpenZeppelin to build an ERC20' rather than just 'ERC20'). |
Implementation Reference
- src/index.ts:165-250 (handler)Implements the core logic of the 'assist_with_cairo' tool: constructs API request with query, code snippets, and history; calls Cairo Coder API via fetch; parses and returns the response or handles errors.private async handleCairoAssistance(args: { query: string; codeSnippets?: string[]; history?: string[]; }) { try { const { query, codeSnippets, history } = args; if (!query) { throw new Error("Query parameter is required"); } let contextualMessage = query; if (codeSnippets && codeSnippets.length > 0) { contextualMessage += `\n\nCode snippets for context:\n${codeSnippets.join("\n\n")}`; } if (history && history.length > 0) { contextualMessage = `Previous conversation context:\n${history.join("\n")}\n\nCurrent query: ${contextualMessage}`; } const requestBody: CairoCoderRequest = { messages: [ { role: "user", content: contextualMessage, }, ], }; // Prepare headers based on mode const headers: Record<string, string> = { "Content-Type": "application/json", mcp: "true", }; // Only add API key header in public API mode if (!this.isLocalMode && this.apiKey) { headers["x-api-key"] = this.apiKey; } const response = await fetch(this.apiUrl, { method: "POST", headers, body: JSON.stringify(requestBody), }); if (!response.ok) { const errorText = await response.text(); throw new Error( `API request failed: ${response.status} ${response.statusText} - ${errorText}`, ); } const data = (await response.json()) as CairoCoderResponse; if (!data.choices || data.choices.length === 0) { throw new Error("No response received from Cairo Coder API"); } const assistantResponse = data.choices[0].message.content; return { content: [ { type: "text", text: assistantResponse, }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : "Unknown error occurred"; return { content: [ { type: "text", text: `Error: ${errorMessage}`, }, ], isError: true, }; } }
- src/index.ts:111-137 (schema)Defines the input schema/validation for the 'assist_with_cairo' tool, specifying properties query (required), codeSnippets (optional array), and history (optional array).inputSchema: { type: "object", properties: { query: { type: "string", description: "The user's question regarding Cairo and Starknet development. Try to be as specific as possible for better results (e.g., 'Using OpenZeppelin to build an ERC20' rather than just 'ERC20').", }, codeSnippets: { type: "array", items: { type: "string", }, description: "Optional: Code snippets for context. This will help the tool understand the user's intent and provide more accurate answers. Provide as much relevant code as possible to fit the user's request.", }, history: { type: "array", items: { type: "string", }, description: "Optional: The preceding conversation history. This can help the tool understand the context of the discussion and provide more accurate answers.", }, }, required: ["query"], },
- src/index.ts:99-141 (registration)Registers the 'assist_with_cairo' tool in the ListToolsRequestSchema handler by returning its metadata: name, description, and inputSchema.this.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ { name: "assist_with_cairo", description: `Provides assistance with Cairo and Starknet development tasks through AI-powered analysis. Call this tool when the user's request involves **writing, refactoring, implementing from scratch, or completing specific parts (like TODOs)** of Cairo code or smart contracts. The tool analyzes the query and context against Cairo/Starknet best practices and documentation, returning helpful information to generate accurate code or explanations. This tool should also be called to get a better understanding of Starknet's ecosystem, features, and capacities.`, inputSchema: { type: "object", properties: { query: { type: "string", description: "The user's question regarding Cairo and Starknet development. Try to be as specific as possible for better results (e.g., 'Using OpenZeppelin to build an ERC20' rather than just 'ERC20').", }, codeSnippets: { type: "array", items: { type: "string", }, description: "Optional: Code snippets for context. This will help the tool understand the user's intent and provide more accurate answers. Provide as much relevant code as possible to fit the user's request.", }, history: { type: "array", items: { type: "string", }, description: "Optional: The preceding conversation history. This can help the tool understand the context of the discussion and provide more accurate answers.", }, }, required: ["query"], }, } as Tool, ], }; });
- src/index.ts:143-157 (registration)Registers the execution handler for 'assist_with_cairo' in the CallToolRequestSchema by checking the tool name and delegating to handleCairoAssistance.this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; if (name === "assist_with_cairo") { return await this.handleCairoAssistance( args as { query: string; codeSnippets?: string[]; history?: string[]; }, ); } throw new Error(`Unknown tool: ${name}`); });