Codestral fill-in-the-middle completion
codestral_fimFill in missing code between a given prefix and suffix. Use for editor autocomplete, code patching, or structured refactors where boundaries are specified.
Instructions
Fill-in-the-middle code completion with Codestral.
Given prompt (code preceding the cursor) and suffix (code after the cursor),
Codestral writes the middle. Use for editor autocomplete scenarios, code-patching
agents, or structured refactors where you know the target boundaries.
Default stop tokens: [] — let the model decide. Override with stop if needed.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | Code preceding the cursor. | |
| suffix | Yes | Code after the cursor. Can be empty string. | |
| model | No | ||
| stop | No | ||
| temperature | No | ||
| max_tokens | No | ||
| top_p | No | ||
| seed | No | Random seed for deterministic sampling. Maps to Mistral's `random_seed`. |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | ||
| model | Yes | ||
| finish_reason | No | ||
| usage | No |
Implementation Reference
- src/tools-fn.ts:174-238 (registration)Registration of the 'codestral_fim' tool via server.registerTool(). Defines metadata, inputSchema, outputSchema, and the handler callback.
// ========== codestral_fim ========== server.registerTool( "codestral_fim", { title: "Codestral fill-in-the-middle completion", description: [ "Fill-in-the-middle code completion with Codestral.", "", "Given `prompt` (code preceding the cursor) and `suffix` (code after the cursor),", "Codestral writes the middle. Use for editor autocomplete scenarios, code-patching", "agents, or structured refactors where you know the target boundaries.", "", "Default stop tokens: [] — let the model decide. Override with `stop` if needed.", ].join("\n"), inputSchema: { prompt: z.string().min(1).describe("Code preceding the cursor."), suffix: z.string().describe("Code after the cursor. Can be empty string."), model: FimModelSchema.optional(), stop: z.array(z.string()).optional(), ...ChatSamplingParams, }, outputSchema: FimOutputShape, annotations: { title: "Codestral FIM", readOnlyHint: true, destructiveHint: false, idempotentHint: false, openWorldHint: true, }, }, async (input) => { try { const model = input.model ?? DEFAULT_FIM_MODEL; const res = await mistral.fim.complete({ model, prompt: input.prompt, suffix: input.suffix, temperature: input.temperature, maxTokens: input.max_tokens, topP: input.top_p, randomSeed: input.seed, stop: input.stop, }); const choice = res.choices?.[0]; const content = choice?.message?.content ?? ""; const text = typeof content === "string" ? content : JSON.stringify(content); const structured = { text, model, finish_reason: choice?.finishReason ?? undefined, usage: mapUsage(res.usage), }; return { content: [toTextBlock(text)], structuredContent: structured, }; } catch (err) { return errorResult("codestral_fim", err); } } ); - src/tools-fn.ts:69-74 (schema)Output shape for FIM: text, model, finish_reason, usage.
export const FimOutputShape = { text: z.string(), model: z.string(), finish_reason: z.string().optional(), usage: UsageSchema.optional(), }; - src/tools-fn.ts:204-238 (handler)Handler function for codestral_fim: calls mistral.fim.complete() with prompt/suffix/model/params, extracts content, returns structured result.
async (input) => { try { const model = input.model ?? DEFAULT_FIM_MODEL; const res = await mistral.fim.complete({ model, prompt: input.prompt, suffix: input.suffix, temperature: input.temperature, maxTokens: input.max_tokens, topP: input.top_p, randomSeed: input.seed, stop: input.stop, }); const choice = res.choices?.[0]; const content = choice?.message?.content ?? ""; const text = typeof content === "string" ? content : JSON.stringify(content); const structured = { text, model, finish_reason: choice?.finishReason ?? undefined, usage: mapUsage(res.usage), }; return { content: [toTextBlock(text)], structuredContent: structured, }; } catch (err) { return errorResult("codestral_fim", err); } } ); - src/shared.ts:97-103 (helper)errorResult helper: wraps errors into MCP error response format with tool name prefix.
export function errorResult(tool: string, err: unknown) { const message = err instanceof Error ? err.message : String(err); return { content: [toTextBlock(`[mistral-mcp:${tool}] ${message}`)], isError: true as const, }; } - src/shared.ts:114-125 (helper)ChatSamplingParams: shared Zod schema for temperature/max_tokens/top_p/seed used in the FIM tool's inputSchema.
export const ChatSamplingParams = { temperature: z.number().min(0).max(2).optional(), max_tokens: z.number().int().positive().optional(), top_p: z.number().min(0).max(1).optional(), seed: z .number() .int() .optional() .describe( "Random seed for deterministic sampling. Maps to Mistral's `random_seed`." ), };