auth
Authorize access to the AgentOps MCP server by validating the API key, ensuring secure access to AI agent observability and tracing data.
Instructions
Authorize using the AGENTOPS_API_KEY. If the API key is not provided and cannot be found in the directory, ask the user for the API key.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| api_key | No | AgentOps project API key (optional if AGENTOPS_API_KEY environment variable is set) |
Implementation Reference
- src/index.ts:234-292 (handler)Main execution logic for the 'auth' tool: handles API key input (from args or env), calls authWithApiKey, stores JWT, verifies with project fetch, returns formatted success response.case "auth": { const { api_key } = args as { api_key?: string }; // Check if already authenticated if (serverState.isAuthenticated() && !api_key) { return { content: [ { type: "text", text: JSON.stringify( { success: true, message: "Already authenticated", source: "Previously authenticated (likely from environment variable on startup)", }, null, 2, ), }, ], }; } // Try to get API key from environment first, then from parameter const actualApiKey = api_key || process.env["AGENTOPS_API_KEY"]; if (!actualApiKey) { throw new Error( "No project API key available. Please provide a project API key.", ); } const authResult = await authWithApiKey(actualApiKey); if (typeof authResult === "object" && "error" in authResult) { throw new Error(`Authentication failed: ${authResult.error}`); } // Store the JWT token in server state serverState.setJwtToken(authResult); const result = await makeAuthenticatedRequest(`/public/v1/project`); const name = result.name; return { content: [ { type: "text", text: JSON.stringify( { success: true, message: "Authentication successful", project: name, }, null, 2, ), }, ], }; }
- src/index.ts:165-180 (registration)Tool registration in listTools handler: defines name 'auth', description, and inputSchema for API key parameter.{ name: "auth", description: "Authorize using the AGENTOPS_API_KEY. If the API key is not provided and cannot be found in the directory, ask the user for the API key.", inputSchema: { type: "object", properties: { api_key: { type: "string", description: "AgentOps project API key (optional if AGENTOPS_API_KEY environment variable is set)", }, }, required: [], }, },
- src/index.ts:169-179 (schema)Input schema definition for the 'auth' tool, specifying optional 'api_key' string parameter.inputSchema: { type: "object", properties: { api_key: { type: "string", description: "AgentOps project API key (optional if AGENTOPS_API_KEY environment variable is set)", }, }, required: [], },
- src/index.ts:80-96 (helper)Core helper function called by 'auth' handler: exchanges AgentOps API key for JWT bearer token via POST request.async function authWithApiKey(apiKey: string): Promise<string | ErrorResponse> { const data = { api_key: apiKey }; try { const response: AxiosResponse = await axios.post( `${HOST}/public/v1/auth/access_token`, data, ); const bearer = response.data?.bearer; if (!bearer) { throw new Error("No bearer token received from auth endpoint"); } return bearer; } catch (error) { return { error: error instanceof Error ? error.message : String(error) }; } }