Skip to main content
Glama
MarveleE
by MarveleE

rap2_ensure_session

Logs into RAP2 using environment variables and caches the session cookie for subsequent API documentation queries.

Instructions

使用环境参数登录并保存 Cookie 到会话缓存

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • Tool handler logic: creates Rap2Client, invokes login() to ensure session, returns success payload with cookie status.
    if (name === 'rap2_ensure_session') {
      const client = createClient();
      const result = await client.login();
      if (result?.error) throw new Error(String(result.error));
      const cookies = client.getCookies();
      const payload = { ok: true, cookies: { hasSid: !!cookies.sid, hasSidSig: !!cookies.sidSig } };
      logger.info({ tool: name, result: payload }, 'tool success');
      return { content: [{ type: 'text', text: JSON.stringify(payload) }] };
    }
  • Tool registration entry in the tools array, including name, description, and input schema.
    {
      name: 'rap2_ensure_session',
      description: '使用环境参数登录并保存 Cookie 到会话缓存',
      inputSchema: {
        type: 'object',
        properties: {},
      },
    },
  • Helper function to create Rap2Client with global session cookie caching and update callbacks.
    function createClient() {
      const cfg = readEnvConfig();
      assertConfig(cfg);
      // 会话级 Cookie 缓存
      if (!globalThis.__RAP2_SESSION__) {
        globalThis.__RAP2_SESSION__ = { sid: cfg.sid || '', sidSig: cfg.sidSig || '' };
      }
      const session = globalThis.__RAP2_SESSION__;
      const client = new Rap2Client({
        ...cfg,
        sid: session.sid,
        sidSig: session.sidSig,
        onCookieUpdate: ({ name, value }) => {
          if (name === 'koa.sid') session.sid = value;
          if (name === 'koa.sid.sig') session.sidSig = value;
        },
        logger,
      });
      return client;
    }
  • Core login implementation in Rap2Client, handles cookie verification, captcha solving loop, and session establishment.
    async login() {
      // 0) If preset cookies exist, try verify
      if (this.cookies['koa.sid'] && this.cookies['koa.sid.sig']) {
        if (this.logger) this.logger.info({ step: 'verify-cookie' }, 'login flow');
        const verify = await this._fetch('/repository/joined', { __skipPreLogin: true });
        if (verify?.data && verify.data.isOk) return { status: '登录成功(使用Cookie)' };
      }
    
      // 1) Warm-up to obtain cookies
      if (this.logger) this.logger.info({ step: 'warmup' }, 'login flow');
      await this._fetch('/account/info', { __skipPreLogin: true });
      if (!this.cookies['koa.sid'] || !this.cookies['koa.sid.sig']) {
        // some instances set cookie only on captcha fetch
      }
    
      // 2) Loop: fetch captcha then read truth from /captcha_data
      const maxAttempts = 6;
      let lastError = null;
      for (let attempt = 1; attempt <= maxAttempts; attempt++) {
        if (this.logger) this.logger.info({ step: 'captcha-loop', attempt }, 'login flow');
        await this._fetch(`/captcha?t=${Date.now()}`, { __skipPreLogin: true });
        const { data: cap } = await this._fetch('/captcha_data', { __skipPreLogin: true });
        let truth = '';
        if (cap && typeof cap === 'object') {
          const raw = cap.data;
          if (typeof raw === 'string') {
            try { truth = JSON.parse(raw).captcha || ''; } catch { truth = ''; }
          } else if (raw && typeof raw === 'object') {
            truth = String(raw.captcha || '');
          }
        }
        if (!truth || truth.length !== 4) { await new Promise(r => setTimeout(r, 600)); continue; }
    
        const payload = JSON.stringify({ email: this.email, password: this.password, captcha: truth.toLowerCase() });
        const loginRes = await this._fetch('/account/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: payload, __skipPreLogin: true });
        const lr = loginRes?.data || {};
        const dataField = lr?.data || {};
        let success = false;
        if (lr?.isOk === true) success = true;
        else if (dataField && (dataField.email || dataField.id)) success = true;
    
        if (success) {
          // verify authorization by hitting a restricted API
          if (this.logger) this.logger.info({ step: 'verify-auth' }, 'login flow');
          const v = await this._fetch('/repository/joined', { __skipPreLogin: true });
          if (v?.data && v.data.isOk === false && String(v.data.errMsg || '').includes('没有访问权限')) {
            // not authorized yet, retry
            await new Promise(r => setTimeout(r, 800));
            continue;
          }
          if (this.logger) this.logger.info({ step: 'login-success' }, 'login flow');
          return { status: '登录成功' };
        }
    
        lastError = String(lr?.errMsg || '登录失败');
        const lower = lastError.toLowerCase();
        if (this.logger) this.logger.warn({ step: 'login-retry', lastError }, 'login flow');
        if (['验证码', 'captcha', 'code', 'verify'].some(k => lower.includes(k))) { await new Promise(r => setTimeout(r, 800)); continue; }
        if (['密码','password','account','邮箱','用户'].some(k => lower.includes(k))) return { error: lastError };
        await new Promise(r => setTimeout(r, 800));
      }
      if (this.logger) this.logger.error({ step: 'login-failed' }, 'login flow');
      return { error: lastError || '登录失败(多次尝试后仍未通过验证码)' };
    }
Behavior2/5

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

No annotations are provided, so the description carries the full burden. It mentions logging in and saving cookies to a session cache, but lacks details on authentication requirements, error handling, rate limits, or what happens if a session already exists. This is a significant gap for a login tool with zero annotation coverage.

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 in Chinese that directly states the tool's function without unnecessary words. It is appropriately sized and front-loaded with the core action.

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?

Given the complexity of a login tool with no annotations and no output schema, the description is incomplete. It doesn't explain the return values, error conditions, or behavioral traits like session management. This leaves gaps for an AI agent to understand how to use it effectively.

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

Parameters4/5

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

The input schema has 0 parameters with 100% coverage, so no parameter documentation is needed. The description implies environment parameters are used for login, which adds context beyond the empty schema, though it doesn't specify what those parameters are. Baseline is 4 for zero parameters.

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 tool's purpose: '使用环境参数登录并保存 Cookie 到会话缓存' (Login using environment parameters and save cookies to session cache). It specifies the verb (login) and resource (session cache), though it doesn't explicitly differentiate from sibling tools like 'rap2_debug_login_info' which might have overlapping functionality.

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. It mentions using environment parameters for login, but doesn't specify scenarios, prerequisites, or exclusions compared to siblings like 'rap2_debug_login_info' or 'rap2_test_connection'.

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/MarveleE/rap2-mcp'

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