Repository Public
1// SP Mail MCP server — stdio transport.2// SPMAIL_API_KEY (required) API key spm_<prefix>_<secret>3// SPMAIL_BASE_URL (optional) default https://mail.spboucher.ai4// SPMAIL_MCP_READONLY=1 (optional) register only the read tools5// stdout carries the MCP protocol: every diagnostic goes to stderr, and the key is never printed.6import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"7import { AuthenticationError, SPMail, SPMailError, type Me } from "@spboucher/spmail"8import { VERSION, buildServer } from "./server.js"910const log = (m: string) => process.stderr.write(`[spmail-mcp] ${m}\n`)11const truthy = (v: string | undefined) => !!v && /^(1|true|yes|on)$/i.test(v.trim())1213async function main() {14 const apiKey = process.env.SPMAIL_API_KEY?.trim()15 if (!apiKey) {16 log("SPMAIL_API_KEY is not set. Create an API key in SP Mail (Administration → API keys) and pass it in the environment of this server.")17 process.exit(2)18 }19 const readonly = truthy(process.env.SPMAIL_MCP_READONLY)20 let client: SPMail21 try {22 client = new SPMail({ apiKey, baseUrl: process.env.SPMAIL_BASE_URL?.trim() || undefined, timeoutMs: 30_000, maxRetries: 2, defaultHeaders: { "user-agent": `spmail-mcp/${VERSION}` } })23 } catch (e) {24 log(`invalid configuration: ${e instanceof Error ? e.message : String(e)}`)25 process.exit(2)26 }27 // The key is no longer needed in the environment of this process (child processes are never spawned, but be strict).28 delete process.env.SPMAIL_API_KEY2930 let me: Me | null = null31 try {32 me = await client.me({ timeoutMs: 10_000, maxRetries: 1 })33 } catch (e) {34 if (e instanceof AuthenticationError) {35 log(`the API key was refused by ${client.baseUrl} (${e.code}). Check SPMAIL_API_KEY.`)36 process.exit(1)37 }38 // Unreachable for now: start anyway with every tool; the API enforces the key's permissions on each call.39 log(`could not reach ${client.baseUrl} at startup (${e instanceof SPMailError ? e.code : "error"}); starting with all tools, permissions are enforced by the API.`)40 }41 const { server, enabled } = buildServer(client, me, { readonly })42 log(`v${VERSION} → ${client.baseUrl}${me ? ` as "${me.name}"` : ""}${readonly ? " [read-only]" : ""}; tools: ${enabled.join(", ")}`)43 const stop = () => { void server.close().finally(() => process.exit(0)) }44 process.on("SIGINT", stop)45 process.on("SIGTERM", stop)46 await server.connect(new StdioServerTransport())47}4849main().catch((e) => {50 log(`fatal: ${e instanceof Error ? e.message : String(e)}`)51 process.exit(1)52})53