Skip to main content
Glama
martechery

Google Ads MCP Server

by martechery

end_session

Terminate a Google Ads API session and remove stored credentials to manage authentication in multi-tenant environments.

Instructions

End a session and clear credentials (multi-tenant mode).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
session_keyYesUUID v4 session key

Implementation Reference

  • Handler function for end_session tool: checks multi-tenant mode, validates session_key, calls endSessionConn to delete the session from connections map, returns success status, and logs events.
    async (input: any) => {
      const startTs = Date.now();
      if (process.env.ENABLE_RUNTIME_CREDENTIALS !== 'true') {
        const out = { content: [{ type: 'text', text: 'Multi-tenant mode not enabled' }] };
        logEvent('end_session', startTs, { requestId: input?.request_id, error: { code: 'ERR_NOT_ENABLED', message: 'Multi-tenant mode not enabled' } });
        return out;
      }
      try {
        validateSessionKey(String(input?.session_key || ''));
      } catch (e: any) {
        const msg = e?.message || String(e);
        logEvent('end_session', startTs, { sessionKey: input?.session_key, requestId: input?.request_id, error: { code: 'ERR_INPUT', message: String(msg) } });
        return { content: [{ type: 'text', text: `Error: ${msg}` }] };
      }
      endSessionConn(String(input.session_key));
      const out = { content: [{ type: 'text', text: JSON.stringify({ status: 'session_ended' }) }] };
      logEvent('end_session', startTs, { sessionKey: input?.session_key, requestId: input?.request_id });
      return out;
  • Registration of the end_session tool using addTool, providing name, description, EndSessionZ schema, and inline handler.
    addTool(
      server,
      'end_session',
      'End a session and clear credentials (multi-tenant mode).',
      EndSessionZ,
      async (input: any) => {
        const startTs = Date.now();
        if (process.env.ENABLE_RUNTIME_CREDENTIALS !== 'true') {
          const out = { content: [{ type: 'text', text: 'Multi-tenant mode not enabled' }] };
          logEvent('end_session', startTs, { requestId: input?.request_id, error: { code: 'ERR_NOT_ENABLED', message: 'Multi-tenant mode not enabled' } });
          return out;
        }
        try {
          validateSessionKey(String(input?.session_key || ''));
        } catch (e: any) {
          const msg = e?.message || String(e);
          logEvent('end_session', startTs, { sessionKey: input?.session_key, requestId: input?.request_id, error: { code: 'ERR_INPUT', message: String(msg) } });
          return { content: [{ type: 'text', text: `Error: ${msg}` }] };
        }
        endSessionConn(String(input.session_key));
        const out = { content: [{ type: 'text', text: JSON.stringify({ status: 'session_ended' }) }] };
        logEvent('end_session', startTs, { sessionKey: input?.session_key, requestId: input?.request_id });
        return out;
      }
    );
  • Zod schema for end_session input: requires session_key as string.
    export const EndSessionZ = z.object({
      session_key: z.string().describe('UUID v4 session key'),
    });
  • Core helper function endSession that deletes the session from the connections Map and emits an event.
    export function endSession(sessionKey: string): void {
      connections.delete(sessionKey);
      try { emitMcpEvent({ timestamp: nowIso(), tool: 'session_ended', session_key: sessionKey, response_time_ms: 0, reason: 'explicit' }); } catch (e) { void e; }
    }

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/martechery/mcp-google-ads-ts'

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