Skip to main content
Glama
martechery

Google Ads MCP Server

by martechery

set_session_credentials

Establish a session with Google Ads credentials in multi-tenant mode to enable API interactions for managing campaigns, retrieving performance data, and executing queries.

Instructions

Establish a session with Google Ads credentials (multi-tenant mode only).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
session_keyYesUUID v4 session key
google_credentialsYes

Implementation Reference

  • Executes the tool logic: checks multi-tenant mode, validates session_key, establishes session with credentials via establishSession helper, optionally verifies Ads API scope, logs telemetry events, returns JSON success or structured error.
      const startTs = Date.now();
      if (process.env.ENABLE_RUNTIME_CREDENTIALS !== 'true') {
        const out = { content: [{ type: 'text', text: 'Multi-tenant mode not enabled' }] };
        logEvent('set_session_credentials', 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('set_session_credentials', startTs, { sessionKey: input?.session_key, requestId: input?.request_id, error: { code: 'ERR_INPUT', message: String(msg) } });
        return { content: [{ type: 'text', text: `Error: ${msg}` }] };
      }
      try {
        const out = establishSession(String(input.session_key), input.google_credentials);
        // Optional: verify Ads scope if enabled
        if (process.env.VERIFY_TOKEN_SCOPE === 'true') {
          try {
            await verifyTokenScopeForSession(String(input.session_key));
          } catch (e: any) {
            const msg = String(e?.message || e);
            if (msg.startsWith('ERR_INSUFFICIENT_SCOPE')) {
              logEvent('set_session_credentials', startTs, { sessionKey: input?.session_key, requestId: input?.request_id, error: { code: 'ERR_INSUFFICIENT_SCOPE', message: 'Missing adwords scope' } });
              return { content: [{ type: 'text', text: JSON.stringify({ error: { code: 'ERR_INSUFFICIENT_SCOPE', message: 'Access token lacks required Google Ads scope (adwords). Please re-authenticate with the correct scope.' } }) }] };
            }
            logEvent('set_session_credentials', startTs, { sessionKey: input?.session_key, requestId: input?.request_id, error: { code: 'ERR_SCOPE_VERIFY_FAILED', message: msg } });
            return { content: [{ type: 'text', text: JSON.stringify({ error: { code: 'ERR_SCOPE_VERIFY_FAILED', message: msg } }) }] };
          }
        }
        const resp = { content: [{ type: 'text', text: JSON.stringify({ status: 'success', ...out }) }] };
        logEvent('set_session_credentials', startTs, { sessionKey: input?.session_key, requestId: input?.request_id });
        return resp;
      } catch (e: any) {
        const msg = e?.message || String(e);
        const code = msg.startsWith('ERR_IMMUTABLE_AUTH') ? 'ERR_IMMUTABLE_AUTH' : 'ERR_ESTABLISH';
        logEvent('set_session_credentials', startTs, { sessionKey: input?.session_key, requestId: input?.request_id, error: { code, message: String(msg) } });
        return { content: [{ type: 'text', text: JSON.stringify({ error: { code, message: msg } }) }] };
      }
    }
  • Zod schema defining input parameters: session_key (UUID) and google_credentials object containing access_token, optional refresh_token, required developer_token, and optional fields.
    export const SetSessionCredentialsZ = z.object({
      session_key: z.string().describe('UUID v4 session key'),
      google_credentials: z.object({
        access_token: z.string(),
        refresh_token: z.string().optional(),
        developer_token: z.string(),
        login_customer_id: z.string().optional(),
        quota_project_id: z.string().optional(),
        expires_at: z.number().optional(),
      }),
    });
  • Registers the tool on the MCP server via addTool(server, name, description, schema, handler), with inline anonymous handler function.
    addTool(
      server,
      'set_session_credentials',
      'Establish a session with Google Ads credentials (multi-tenant mode only).',
      SetSessionCredentialsZ,
      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('set_session_credentials', 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('set_session_credentials', startTs, { sessionKey: input?.session_key, requestId: input?.request_id, error: { code: 'ERR_INPUT', message: String(msg) } });
          return { content: [{ type: 'text', text: `Error: ${msg}` }] };
        }
        try {
          const out = establishSession(String(input.session_key), input.google_credentials);
          // Optional: verify Ads scope if enabled
          if (process.env.VERIFY_TOKEN_SCOPE === 'true') {
            try {
              await verifyTokenScopeForSession(String(input.session_key));
            } catch (e: any) {
              const msg = String(e?.message || e);
              if (msg.startsWith('ERR_INSUFFICIENT_SCOPE')) {
                logEvent('set_session_credentials', startTs, { sessionKey: input?.session_key, requestId: input?.request_id, error: { code: 'ERR_INSUFFICIENT_SCOPE', message: 'Missing adwords scope' } });
                return { content: [{ type: 'text', text: JSON.stringify({ error: { code: 'ERR_INSUFFICIENT_SCOPE', message: 'Access token lacks required Google Ads scope (adwords). Please re-authenticate with the correct scope.' } }) }] };
              }
              logEvent('set_session_credentials', startTs, { sessionKey: input?.session_key, requestId: input?.request_id, error: { code: 'ERR_SCOPE_VERIFY_FAILED', message: msg } });
              return { content: [{ type: 'text', text: JSON.stringify({ error: { code: 'ERR_SCOPE_VERIFY_FAILED', message: msg } }) }] };
            }
          }
          const resp = { content: [{ type: 'text', text: JSON.stringify({ status: 'success', ...out }) }] };
          logEvent('set_session_credentials', startTs, { sessionKey: input?.session_key, requestId: input?.request_id });
          return resp;
        } catch (e: any) {
          const msg = e?.message || String(e);
          const code = msg.startsWith('ERR_IMMUTABLE_AUTH') ? 'ERR_IMMUTABLE_AUTH' : 'ERR_ESTABLISH';
          logEvent('set_session_credentials', startTs, { sessionKey: input?.session_key, requestId: input?.request_id, error: { code, message: String(msg) } });
          return { content: [{ type: 'text', text: JSON.stringify({ error: { code, message: msg } }) }] };
        }
      }
    );
  • Key helper called by the handler: stores/replaces session credentials in a Map-based store, configures per-session customer allowlist and rate limiter, handles overwrite policy, starts idle sweeper, emits telemetry, returns TTL info.
    export function establishSession(sessionKey: string, credentials: GoogleCredential): { session_key: string; expires_in: number; overwritten?: boolean } {
      if (!isMultiTenantEnabled()) {
        throw new Error('Multi-tenant mode not enabled');
      }
      validateSessionKey(sessionKey);
      if (!credentials.developer_token) {
        throw new Error('Developer token required in multi-tenant mode');
      }
    
      const allowedIds = process.env.ALLOWED_CUSTOMER_IDS
        ? new Set(
            process.env.ALLOWED_CUSTOMER_IDS
              .split(',')
              .map((s) => s.trim())
              .filter(Boolean)
              .map((id) => formatCustomerId(id))
          )
        : undefined;
    
      const now = Date.now();
      const existed = connections.has(sessionKey);
      if (existed) {
        if (process.env.STRICT_IMMUTABLE_AUTH === 'true') {
          throw new Error('ERR_IMMUTABLE_AUTH: Authentication cannot be modified for this session');
        }
        // Non-strict: allow overwrite (for recovery) and log via caller
        const ctx = connections.get(sessionKey)!;
        ctx.credentials = credentials;
        ctx.lastActivityAt = now;
        ctx.allowedCustomerIds = allowedIds;
        // best-effort log
        try { emitMcpEvent({ timestamp: nowIso(), tool: 'session_established', session_key: sessionKey, response_time_ms: 0, overwritten: true }); } catch (e) { void e; }
      } else {
        const rl = isRateLimitingEnabled()
          ? new TokenBucket(
              parseInt(process.env.RATE_LIMIT_BURST || '20', 10),
              parseFloat(process.env.REQUESTS_PER_SECOND || '10')
            )
          : undefined;
        connections.set(sessionKey, {
          session_key: sessionKey,
          credentials,
          establishedAt: now,
          lastActivityAt: now,
          allowedCustomerIds: allowedIds,
          rateLimiter: rl,
        });
        totalSessionCount++;
        try { emitMcpEvent({ timestamp: nowIso(), tool: 'session_established', session_key: sessionKey, response_time_ms: 0, overwritten: false }); } catch (e) { void e; }
      }
      startConnectionSweeper();
    
      const ttlSec = parseInt(process.env.RUNTIME_CREDENTIAL_TTL || '3600', 10);
      return { session_key: sessionKey, expires_in: ttlSec, overwritten: existed };
    }
Behavior2/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It states this establishes a session but doesn't describe what that session enables, how long it lasts, whether it's persistent, what permissions are required, or what happens if credentials are invalid. For a credential management tool with zero annotation coverage, this leaves significant behavioral gaps.

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 communicates the core purpose without unnecessary words. It's appropriately sized and front-loaded with the essential information.

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 credential/session management tool with no annotations, no output schema, and incomplete parameter documentation, the description is insufficient. It doesn't explain what a successful establishment returns, what errors might occur, or how this session integrates with other tools. The 'multi-tenant mode only' constraint is helpful but doesn't compensate for other missing context.

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?

With 50% schema description coverage (only 'session_key' has a description), the description doesn't add any parameter-specific information beyond what's in the schema. It mentions 'Google Ads credentials' which aligns with the 'google_credentials' parameter but doesn't explain the structure or purpose of the credential fields. The description doesn't compensate for the schema coverage gap.

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 ('Establish a session') and target ('with Google Ads credentials'), and specifies the operational mode ('multi-tenant mode only'). It doesn't explicitly differentiate from sibling tools like 'manage_auth' or 'refresh_access_token', but the specificity of establishing a session with Google Ads credentials provides good clarity.

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

Usage Guidelines3/5

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

The description implies usage context ('multi-tenant mode only') but doesn't provide explicit guidance on when to use this tool versus alternatives like 'manage_auth' or 'refresh_access_token'. It suggests this is for initial session establishment but doesn't clarify prerequisites or when other tools might be more appropriate.

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

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