Codestral fill-in-the-middle completion
codestral_fimComplete code between a given prefix and suffix. Use for editor autocomplete, code patching, or structured refactors where boundaries are known.
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:204-238 (handler)The async handler that executes the codestral_fim logic: calls mistral.fim.complete() with prompt/suffix/model/params, then maps the response to structured output with text, model, finish_reason, and usage.
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:188-194 (schema)Input schema for codestral_fim: prompt (required), suffix (required), model (optional FIM model), stop tokens, plus shared ChatSamplingParams (temperature, max_tokens, top_p, seed). Output schema (FimOutputShape) includes text, model, finish_reason, and optional usage.
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, }, - src/tools-fn.ts:175-238 (registration)Registration of the 'codestral_fim' tool via server.registerTool() within the registerFunctionTools() function. It's called from src/index.ts:66 with the MCP server, Mistral client, and profile.
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-75 (schema)Output shape and schema for codestral_fim (and also re-used elsewhere). Defines the structured response fields: text (string), model (string), finish_reason (optional), usage (optional UsageSchema).
export const FimOutputShape = { text: z.string(), model: z.string(), finish_reason: z.string().optional(), usage: UsageSchema.optional(), }; export const FimOutputSchema = z.object(FimOutputShape); - src/models.ts:70-106 (helper)FIM model definitions: the only FIM-compatible model is 'codestral-latest', and DEFAULT_FIM_MODEL is used as fallback in the handler.
export const FIM_MODELS = ["codestral-latest"] as const; /** * Function-calling-capable models, per Mistral docs. * Source: https://docs.mistral.ai/capabilities/function_calling/ — "Available Models" block. */ export const TOOL_CAPABLE_MODELS = [ "mistral-large-latest", "mistral-medium-latest", "mistral-small-latest", "ministral-3b-latest", "ministral-8b-latest", "ministral-14b-latest", "magistral-medium-latest", "magistral-small-latest", "devstral-latest", "devstral-small-latest", "codestral-latest", "voxtral-small-latest", ] as const; export const ChatModelSchema = z.enum(CHAT_MODELS); export const EmbedModelSchema = z.enum(EMBED_MODELS); export const FimModelSchema = z.enum(FIM_MODELS); export const ToolModelSchema = z.enum(TOOL_CAPABLE_MODELS); export const VisionModelSchema = z.enum(VISION_MODELS); export const OcrModelSchema = z.enum(OCR_MODELS); export const SttModelSchema = z.enum(STT_MODELS); export const ModerationModelSchema = z.enum(MODERATION_MODELS); export const DEFAULT_CHAT_MODEL: (typeof CHAT_MODELS)[number] = "mistral-medium-latest"; export const DEFAULT_EMBED_MODEL: (typeof EMBED_MODELS)[number] = "mistral-embed"; export const DEFAULT_FIM_MODEL: (typeof FIM_MODELS)[number] = "codestral-latest"; export const DEFAULT_TOOL_MODEL: (typeof TOOL_CAPABLE_MODELS)[number] = "mistral-medium-latest";