Skip to main content
Glama
georgejeffers

Gemini MCP Server

Chat

chat

Initiate or continue multi-turn conversations with Gemini AI models through session management. Send messages to start new discussions or resume previous ones using session IDs.

Instructions

Multi-turn conversation with session management. Omit sessionId to start a new session; include it to continue an existing one.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
messageYesThe message to send
sessionIdNoSession ID from a previous response to continue that conversation
modelNoGemini model to usegemini-2.5-flash
systemInstructionNoSystem instruction (only applied when starting a new session)

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
responseYesThe model response text
sessionIdYesSession ID for continuing this conversation
turnCountYesNumber of turns in this session

Implementation Reference

  • The main handler function that implements the chat tool logic, including session creation/retrieval, message sending to AI, and response formatting.
    async ({ message, sessionId, model, systemInstruction }) => {
      try {
        let session = sessionId ? getSession(sessionId) : undefined;
        const id = session ? sessionId! : generateSessionId();
    
        if (!session) {
          const chat = ai.chats.create({
            model,
            config: systemInstruction ? { systemInstruction } : undefined,
          });
          session = { chat, turnCount: 0, lastActivity: Date.now() };
          storeSession(id, session);
        }
    
        const response = await session.chat.sendMessage({ message });
        session.turnCount++;
        session.lastActivity = Date.now();
    
        const result = {
          sessionId: id,
          response: response.text ?? '',
          turnCount: session.turnCount,
        };
    
        return {
          structuredContent: result,
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      } catch (error) {
        return formatToolError(error);
      }
    },
  • Input/output schema definitions for the chat tool, including validation for message, sessionId, model, and systemInstruction inputs, and sessionId, response, and turnCount outputs.
    inputSchema: {
      message: z.string().min(1).describe('The message to send'),
      sessionId: z.string().optional().describe('Session ID from a previous response to continue that conversation'),
      model: TextModel.default('gemini-2.5-flash').describe('Gemini model to use'),
      systemInstruction: z.string().optional().describe('System instruction (only applied when starting a new session)'),
    },
    outputSchema: {
      sessionId: z.string().describe('Session ID for continuing this conversation'),
      response: z.string().describe('The model response text'),
      turnCount: z.number().describe('Number of turns in this session'),
    },
    annotations: {
  • src/tools/chat.ts:8-64 (registration)
    Tool registration function that registers the 'chat' tool with the MCP server, defining its metadata, schema, and handler.
    export function register(server: McpServer, ai: GoogleGenAI): void {
      server.registerTool(
        'chat',
        {
          title: 'Chat',
          description: 'Multi-turn conversation with session management. Omit sessionId to start a new session; include it to continue an existing one.',
          inputSchema: {
            message: z.string().min(1).describe('The message to send'),
            sessionId: z.string().optional().describe('Session ID from a previous response to continue that conversation'),
            model: TextModel.default('gemini-2.5-flash').describe('Gemini model to use'),
            systemInstruction: z.string().optional().describe('System instruction (only applied when starting a new session)'),
          },
          outputSchema: {
            sessionId: z.string().describe('Session ID for continuing this conversation'),
            response: z.string().describe('The model response text'),
            turnCount: z.number().describe('Number of turns in this session'),
          },
          annotations: {
            readOnlyHint: false,
            destructiveHint: false,
            openWorldHint: true,
          },
        },
        async ({ message, sessionId, model, systemInstruction }) => {
          try {
            let session = sessionId ? getSession(sessionId) : undefined;
            const id = session ? sessionId! : generateSessionId();
    
            if (!session) {
              const chat = ai.chats.create({
                model,
                config: systemInstruction ? { systemInstruction } : undefined,
              });
              session = { chat, turnCount: 0, lastActivity: Date.now() };
              storeSession(id, session);
            }
    
            const response = await session.chat.sendMessage({ message });
            session.turnCount++;
            session.lastActivity = Date.now();
    
            const result = {
              sessionId: id,
              response: response.text ?? '',
              turnCount: session.turnCount,
            };
    
            return {
              structuredContent: result,
              content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
            };
          } catch (error) {
            return formatToolError(error);
          }
        },
      );
    }
  • Helper utilities for chat session management: ChatSession interface, session storage Map, and functions for generating/retrieving/storing sessions.
    export interface ChatSession {
      chat: any;
      turnCount: number;
      lastActivity: number;
    }
    
    const sessions = new Map<string, ChatSession>();
    const SESSION_TIMEOUT = 30 * 60 * 1000; // 30 minutes
    
    export function createClient(apiKey: string): GoogleGenAI {
      return new GoogleGenAI({ apiKey });
    }
    
    export function generateSessionId(): string {
      return crypto.randomUUID();
    }
    
    export function getSession(id: string): ChatSession | undefined {
      const session = sessions.get(id);
      if (session) {
        session.lastActivity = Date.now();
      }
      return session;
    }
    
    export function storeSession(id: string, session: ChatSession): void {
      sessions.set(id, session);
    }
    
    export function deleteSession(id: string): boolean {
      return sessions.delete(id);
    }
  • src/index.ts:26-26 (registration)
    Main server registration point where the chat tool is registered with the MCP server instance.
    registerChat(server, ai);
Behavior4/5

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

The description adds valuable context beyond annotations: it explains session management behavior (starting vs. continuing conversations) and hints at statefulness. Annotations cover safety (readOnlyHint=false, destructiveHint=false, openWorldHint=true), so the bar is lower, but the description provides operational insights that enhance understanding of how the tool behaves in practice.

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 extremely concise and front-loaded, consisting of just two sentences that efficiently convey the core functionality and key usage rule. Every sentence earns its place by addressing essential aspects without any wasted words.

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

Completeness5/5

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

Given the tool's complexity (multi-turn conversation with session management), rich annotations, 100% schema coverage, and the presence of an output schema, the description is complete enough. It focuses on the unique behavioral aspects (session management) while relying on structured fields for parameter and output details.

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 the schema fully documents all parameters. The description adds minimal parameter semantics by clarifying sessionId usage, but it doesn't provide additional meaning beyond what's in the schema descriptions. This meets the baseline for high schema coverage.

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 the tool's purpose as 'Multi-turn conversation with session management,' which is a specific verb+resource combination. It distinguishes this tool from sibling tools like code_execution or generate_text by emphasizing conversation continuity rather than single-turn generation or other modalities.

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

Usage Guidelines5/5

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

The description provides explicit guidance on when to use this tool: 'Omit sessionId to start a new session; include it to continue an existing one.' This directly addresses the key decision point for usage versus alternatives, offering clear context for session management without needing exclusions.

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/georgejeffers/gemini-mcp-server'

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