"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.createOAuthServer = createOAuthServer;
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
const zod_1 = require("zod");
const logger_1 = require("../../lib/logger");
const oauth = __importStar(require("../activities/oauth"));
const getAccessTokenSchema = zod_1.z.object({
    provider: zod_1.z.string().describe('OAuth provider name (google, github, microsoft, anthropic, etc.)'),
    user_id: zod_1.z.string().describe('User ID to get token for'),
    label: zod_1.z.string().optional().describe('Credential label (default: "default"). Use to select among multiple credentials for the same provider.'),
});
const listConnectionsSchema = zod_1.z.object({
    user_id: zod_1.z.string().describe('User ID to list connections for'),
});
const revokeConnectionSchema = zod_1.z.object({
    provider: zod_1.z.string().describe('OAuth provider name to disconnect'),
    user_id: zod_1.z.string().describe('User ID to revoke connection for'),
    label: zod_1.z.string().optional().describe('Credential label to revoke (default: "default")'),
});
/**
 * Create an OAuth MCP server.
 *
 * Provides 3 tools for OAuth token management:
 *   get_access_token, list_connections, revoke_connection
 *
 * Workflows call get_access_token to get fresh credentials
 * before making authenticated API calls to external services.
 */
async function createOAuthServer() {
    const instance = new mcp_js_1.McpServer({ name: 'long-tail-oauth', version: '1.0.0' });
    instance.registerTool('get_access_token', {
        title: 'Get OAuth Access Token',
        description: 'Get a fresh OAuth access token for an external service (Google, GitHub, etc.). ' +
            'Automatically refreshes expired tokens. Call this immediately before making an ' +
            'authenticated API request — do not cache or reuse across workflow steps. ' +
            'Use the label parameter to select a specific credential when multiple exist.',
        inputSchema: getAccessTokenSchema,
    }, async (args) => {
        const result = await oauth.getAccessToken(args);
        return {
            content: [{ type: 'text', text: JSON.stringify(result) }],
        };
    });
    instance.registerTool('list_connections', {
        title: 'List OAuth Connections',
        description: 'List all OAuth providers connected for a user. Returns provider, label, and credential type for each connection.',
        inputSchema: listConnectionsSchema,
    }, async (args) => {
        const result = await oauth.listConnections(args);
        return {
            content: [{ type: 'text', text: JSON.stringify(result) }],
        };
    });
    instance.registerTool('revoke_connection', {
        title: 'Revoke OAuth Connection',
        description: 'Disconnect an OAuth provider for a user, removing stored tokens. Use label to target a specific credential.',
        inputSchema: revokeConnectionSchema,
    }, async (args) => {
        const result = await oauth.revokeConnection(args);
        return {
            content: [{ type: 'text', text: JSON.stringify(result) }],
        };
    });
    logger_1.loggerRegistry.info('[lt-mcp:oauth] long-tail-oauth ready (3 tools registered)');
    return instance;
}
