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
| Name | Required | Description | Default |
|---|---|---|---|
| session_key | Yes | UUID v4 session key |
Implementation Reference
- src/server-tools.ts:826-843 (handler)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;
- src/server-tools.ts:821-845 (registration)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; } );
- src/schemas.ts:90-92 (schema)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; } }