Skip to main content
Glama
th3nolo

OpenRouter MCP Server

by th3nolo

chat_with_model

Send messages to AI models via OpenRouter to generate responses, compare outputs, and retrieve model information with pricing details.

Instructions

Send a message to a specific OpenRouter model

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
modelYesOpenRouter model ID (e.g., 'openai/gpt-4')
messageYesMessage to send to the model
max_tokensNoMaximum tokens in response
temperatureNoTemperature for response randomness
system_promptNoSystem prompt for the conversation

Implementation Reference

  • The main handler function that executes the chat_with_model tool. It validates parameters using ChatRequestSchema, constructs the messages array (including optional system prompt), makes an API call to OpenRouter's chat/completions endpoint, and returns the response with usage statistics.
    private async chatWithModel(params: z.infer<typeof ChatRequestSchema>) {
      const { model, message, max_tokens, temperature, system_prompt } = params;
    
      const messages = [];
      if (system_prompt) {
        messages.push({ role: "system", content: system_prompt });
      }
      messages.push({ role: "user", content: message });
    
      const response = await axios.post(
        `${OPENROUTER_CONFIG.baseURL}/chat/completions`,
        {
          model,
          messages,
          max_tokens,
          temperature,
        },
        { headers: OPENROUTER_CONFIG.headers }
      );
    
      const result = response.data.choices[0].message.content;
      const usage = response.data.usage;
    
      return {
        content: [
          {
            type: "text" as const,
            text: `**Model:** ${model}\n**Response:** ${result}\n\n**Usage:**\n- Prompt tokens: ${usage.prompt_tokens}\n- Completion tokens: ${usage.completion_tokens}\n- Total tokens: ${usage.total_tokens}`,
          },
        ],
      };
    }
  • Zod schema defining the input validation for chat_with_model tool. Includes required fields (model, message) and optional fields (max_tokens, temperature, system_prompt) with defaults.
    const ChatRequestSchema = z.object({
      model: z.string().describe("OpenRouter model ID (e.g., 'openai/gpt-4')"),
      message: z.string().describe("Message to send to the model"),
      max_tokens: z.number().optional().default(1000).describe("Maximum tokens in response"),
      temperature: z.number().optional().default(0.7).describe("Temperature for response randomness"),
      system_prompt: z.string().optional().describe("System prompt for the conversation"),
    });
  • src/server.ts:145-176 (registration)
    Tool registration in the ListToolsRequestSchema handler. Defines the tool's metadata, input schema structure, required parameters, and default values for MCP clients to discover.
    {
      name: "chat_with_model",
      description: "Send a message to a specific OpenRouter model",
      inputSchema: {
        type: "object",
        properties: {
          model: {
            type: "string",
            description: "OpenRouter model ID (e.g., 'openai/gpt-4')",
          },
          message: {
            type: "string",
            description: "Message to send to the model",
          },
          max_tokens: {
            type: "number",
            description: "Maximum tokens in response",
            default: 1000,
          },
          temperature: {
            type: "number",
            description: "Temperature for response randomness",
            default: 0.7,
          },
          system_prompt: {
            type: "string",
            description: "System prompt for the conversation",
          },
        },
        required: ["model", "message"],
      },
    },
  • src/server.ts:229-230 (registration)
    Tool dispatch logic in the CallToolRequestSchema handler. Routes the chat_with_model tool invocation to the chatWithModel handler method with parameter validation.
    case "chat_with_model":
      return await this.chatWithModel(ChatRequestSchema.parse(args));
  • Configuration object used by the chatWithModel handler for API requests to OpenRouter. Contains base URL, API key, and headers required for authentication.
    const OPENROUTER_CONFIG = {
      baseURL: process.env.OPENROUTER_BASE_URL || "https://openrouter.ai/api/v1",
      apiKey: process.env.OPENROUTER_API_KEY,
      headers: {
        "Authorization": `Bearer ${process.env.OPENROUTER_API_KEY}`,
        "HTTP-Referer": process.env.OPENROUTER_SITE_URL || "http://localhost:3000",
        "X-Title": process.env.OPENROUTER_APP_NAME || "OpenRouter MCP Server",
        "Content-Type": "application/json",
      },
    };
Behavior2/5

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

With no annotations provided, the description carries full burden for behavioral disclosure. It states the basic action but reveals nothing about authentication requirements, rate limits, cost implications, response format, error handling, or whether this initiates a new conversation versus continues an existing one. For a tool that likely involves API calls with potential costs, this is a significant gap.

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 a single, efficient sentence that states the core functionality without any unnecessary words. It's perfectly front-loaded with the essential information, making it immediately clear what the tool does.

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

Completeness2/5

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

For a tool with 5 parameters, no annotations, and no output schema, the description is insufficient. It doesn't explain what the tool returns, how responses are structured, or important behavioral aspects like conversation state management. The agent would need to guess about the response format and operational characteristics.

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

Parameters3/5

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

Schema description coverage is 100%, so all parameters are documented in the schema. The description adds no additional parameter semantics beyond what's already in the structured schema, which provides clear descriptions for each parameter including defaults for optional ones. This meets the baseline expectation when schema coverage is complete.

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

Purpose4/5

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

The description clearly states the action ('Send a message') and target resource ('specific OpenRouter model'), making the purpose immediately understandable. However, it doesn't differentiate from sibling tools like 'compare_models' or 'get_model_info', which have different purposes but operate on the same resource domain.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool versus alternatives like 'compare_models' or 'list_models'. There's no mention of prerequisites, appropriate contexts, or exclusion criteria, leaving the agent to infer usage from the tool name alone.

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/th3nolo/openrouter-mcp'

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