/**
* LinkedIn Auth Tool - Required first tool for each session
*/
import { z } from "zod";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { authenticate } from "../session/manager.js";
import { clearClient, getClient } from "../api/client.js";
// Input Schema
const InputSchema = z.object({
force: z.boolean().optional().describe("Force re-authentication even if session exists"),
}).strict();
// Output Schema
const OutputSchema = z.object({
success: z.boolean().describe("Whether authentication was successful"),
user: z.string().optional().describe("Authenticated user's name"),
profileUrl: z.string().optional().describe("Authenticated user's profile URL"),
sessionId: z.string().optional().describe("Session ID to use with other tools"),
expiresAt: z.string().optional().describe("ISO timestamp when session expires"),
error: z.string().optional().describe("Error message if authentication failed"),
}).strict();
const DESCRIPTION = `Authenticate with LinkedIn by extracting cookies from Chrome.
**Requirements:**
- You must be logged into LinkedIn in Chrome
Returns:
- sessionId: Use this ID with all other LinkedIn tools
- expiresAt: Session expiration time`;
export function registerAuthTool(server: McpServer): void {
server.registerTool(
"linkedin_auth",
{
title: "LinkedIn Auth",
description: DESCRIPTION,
inputSchema: InputSchema,
outputSchema: OutputSchema,
annotations: {
title: "LinkedIn Authentication",
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: false,
},
},
async () => {
try {
clearClient();
const result = await authenticate();
if (result.success && result.sessionId) {
let userName = "";
let profileUrl = "";
try {
const client = getClient(result.sessionId);
const profile = await client.getMyProfile();
userName = profile.name;
profileUrl = profile.profileUrl;
} catch {
// Profile fetch failed, continue without it
}
return {
content: [{
type: "text" as const,
text: `✅ **Authenticated Successfully**
${userName ? `**User:** ${userName}` : ""}
${profileUrl ? `**Profile:** ${profileUrl}` : ""}
**Session ID:** \`${result.sessionId}\`
**Expires:** ${result.expiresAt}
Use this session_id with all other LinkedIn tools:
- linkedin_search
- linkedin_get
- view_my_profile`,
}],
structuredContent: {
success: true,
user: userName || undefined,
profileUrl: profileUrl || undefined,
sessionId: result.sessionId,
expiresAt: result.expiresAt,
},
};
}
return {
content: [{ type: "text" as const, text: `❌ **Authentication Failed**\n\n${result.error}` }],
structuredContent: { success: false, error: result.error },
isError: true,
};
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error);
return {
content: [{ type: "text" as const, text: `Error: ${errorMsg}` }],
structuredContent: { success: false, error: errorMsg },
isError: true,
};
}
}
);
}