Skip to main content
Glama

Codestral fill-in-the-middle completion

codestral_fim
Read-only

Complete 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

TableJSON Schema
NameRequiredDescriptionDefault
promptYesCode preceding the cursor.
suffixYesCode after the cursor. Can be empty string.
modelNo
stopNo
temperatureNo
max_tokensNo
top_pNo
seedNoRandom seed for deterministic sampling. Maps to Mistral's `random_seed`.

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
textYes
modelYes
finish_reasonNo
usageNo

Implementation Reference

  • 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);
        }
      }
    );
  • 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,
    },
  • 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);
        }
      }
    );
  • 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);
  • 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";
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already declare readOnlyHint=true and destructiveHint=false, establishing safety. The description adds context about stop token behavior and the model's default behavior, complementing annotations. No contradictions.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is concise with two short paragraphs, front-loading purpose and usage. Every sentence adds value; no redundancy or fluff.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Core functionality is well explained, but with 8 parameters and an output schema, the description omits details on most parameters, leaving the tool partially opaque. The presence of annotations and output schema somewhat compensates.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters2/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is low (38%), and the description only explains prompt, suffix, and stop tokens. Many parameters (model, temperature, max_tokens, top_p, seed) are left undocumented, failing to compensate for the schema gap.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states it is fill-in-the-middle code completion, using prompt and suffix, and specifies use cases like editor autocomplete, code-patching, and structured refactors. This distinguishes it from sibling tools like mistral_chat or mistral_ocr.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Explicitly lists when to use (autocomplete, code-patching, refactors) and mentions default stop tokens with override guidance. However, it does not explicitly exclude usage for other tasks (e.g., chat) but sibling tools cover those.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Swih/mistral-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server