create_embeddings
Generate embeddings for text using the Grok API, specifying model, input, encoding format, and dimensions for accurate vector representations.
Instructions
Create embeddings for text with the Grok API
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| dimensions | No | The number of dimensions the resulting output embeddings should have | |
| encoding_format | No | The format to return the embeddings in | |
| input | Yes | Input text to get embeddings for | |
| model | Yes | ID of the model to use | |
| user | No | A unique user identifier |
Implementation Reference
- src/operations/embeddings.ts:47-56 (handler)Core handler function implementing the embedding creation logic by making a POST request to the Grok embeddings endpoint and parsing the response.export async function createEmbeddings( options: z.infer<typeof EmbeddingsRequestSchema> ): Promise<z.infer<typeof EmbeddingsResponseSchema>> { const response = await grokRequest("embeddings", { method: "POST", body: options, }); return EmbeddingsResponseSchema.parse(response); }
- src/operations/embeddings.ts:21-44 (schema)Zod schema defining the input parameters for the create_embeddings tool.export const EmbeddingsRequestSchema = z.object({ model: z.string().describe("ID of the model to use"), input: z .union([ z.string(), z.array(z.string()), z.array(z.number()), z.array(z.array(z.number())), ]) .describe("Input text to get embeddings for"), encoding_format: z .enum(["float", "base64"]) .optional() .describe("The format to return the embeddings in"), dimensions: z .number() .int() .positive() .optional() .describe( "The number of dimensions the resulting output embeddings should have" ), user: z.string().optional().describe("A unique user identifier"), });
- index.ts:138-150 (registration)Registration of the create_embeddings tool on the MCP server, including the thin wrapper execute function that calls the core handler.server.addTool({ name: "create_embeddings", description: "Create embeddings for text with the Grok API", parameters: embeddings.EmbeddingsRequestSchema, execute: async (args) => { try { const result = await embeddings.createEmbeddings(args); return JSON.stringify(result, null, 2); } catch (err) { handleError(err); } }, });
- src/operations/embeddings.ts:11-19 (schema)Zod schema for parsing the response from the embeddings API.export const EmbeddingsResponseSchema = z.object({ object: z.literal("list"), data: z.array(EmbeddingObjectSchema), model: z.string(), usage: z.object({ prompt_tokens: z.number(), total_tokens: z.number(), }), });
- src/operations/embeddings.ts:5-9 (schema)Zod schema for individual embedding objects in the response.export const EmbeddingObjectSchema = z.object({ object: z.literal("embedding"), embedding: z.array(z.number()), index: z.number(), });