Files
exchange-mcp/tools/auth.ts
T
2026-07-09 01:12:40 +03:00

93 lines
2.8 KiB
TypeScript

import { z } from "zod/v4";
import { EwsClient, loadConfig, saveConfig, clearConfig } from "../ews_client.ts";
import { type McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
export function registerAuthTools(server: McpServer): void {
server.registerTool(
"login",
{
description: "Authenticate to Exchange EWS using NTLM credentials",
inputSchema: z.object({
serverUrl: z.string().describe("EWS server URL (e.g. https://mail.example.com)"),
email: z.string().describe("Email address"),
username: z.string().describe("NTLM username"),
password: z.string().describe("NTLM password"),
domain: z.string().optional().describe("NTLM domain (default: corp)"),
}),
},
async ({ serverUrl, email, username, password, domain }) => {
try {
const config = {
serverUrl: serverUrl.replace(/\/+$/, ""),
email,
username,
password,
domain: domain ?? "corp",
};
const client = new EwsClient(config);
const ok = await client.verifyConnection();
if (!ok) {
return {
content: [{ type: "text" as const, text: JSON.stringify({ success: false, error: "Connection verification failed. Check credentials and server URL." }) }],
};
}
saveConfig(config);
return {
content: [{ type: "text" as const, text: JSON.stringify({ success: true, message: `Logged in as ${email} to ${serverUrl}` }) }],
};
} catch (error: any) {
return {
content: [{ type: "text" as const, text: JSON.stringify({ success: false, error: error.message ?? String(error) }) }],
};
}
},
);
server.registerTool(
"check_session",
{
description: "Check whether the current EWS session is authenticated",
inputSchema: z.object({}),
},
async () => {
try {
const config = loadConfig();
const client = new EwsClient(config);
const ok = await client.verifyConnection();
return {
content: [{
type: "text" as const,
text: JSON.stringify({
authenticated: ok,
email: config.email,
serverUrl: config.serverUrl,
}),
}],
};
} catch (error: any) {
return {
content: [{ type: "text" as const, text: JSON.stringify({ authenticated: false, error: error.message ?? String(error) }) }],
};
}
},
);
server.registerTool(
"logout",
{
description: "Clear stored credentials",
inputSchema: z.object({}),
},
async () => {
clearConfig();
return {
content: [{ type: "text" as const, text: JSON.stringify({ success: true, message: "Logged out. Credentials cleared." }) }],
};
},
);
}