|
| 1 | +import { Command } from "interactive-commander"; |
| 2 | +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
| 3 | +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 4 | +import Z from "zod"; |
| 5 | +import { ReplexicaEngine } from "@lingo.dev/_sdk"; |
| 6 | +import { getSettings } from "../utils/settings"; |
| 7 | +import { createAuthenticator } from "../utils/auth"; |
| 8 | + |
| 9 | +export default new Command() |
| 10 | + .command("mcp") |
| 11 | + .description("Use Lingo.dev model context provider with your AI agent") |
| 12 | + .helpOption("-h, --help", "Show help") |
| 13 | + .action(async (_, program) => { |
| 14 | + const apiKey = program.args[0]; |
| 15 | + const settings = getSettings(apiKey); |
| 16 | + |
| 17 | + if (!settings.auth.apiKey) { |
| 18 | + console.error("No API key provided"); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + const authenticator = createAuthenticator({ |
| 23 | + apiUrl: settings.auth.apiUrl, |
| 24 | + apiKey: settings.auth.apiKey!, |
| 25 | + }); |
| 26 | + const auth = await authenticator.whoami(); |
| 27 | + |
| 28 | + if (!auth) { |
| 29 | + console.error("Not authenticated"); |
| 30 | + return; |
| 31 | + } else { |
| 32 | + console.log(`Authenticated as ${auth.email}`); |
| 33 | + } |
| 34 | + |
| 35 | + const replexicaEngine = new ReplexicaEngine({ |
| 36 | + apiKey: settings.auth.apiKey, |
| 37 | + apiUrl: settings.auth.apiUrl, |
| 38 | + }); |
| 39 | + |
| 40 | + const server = new McpServer({ |
| 41 | + name: "Lingo.dev", |
| 42 | + version: "1.0.0", |
| 43 | + }); |
| 44 | + |
| 45 | + server.tool( |
| 46 | + "translate", |
| 47 | + "Detect language and translate text with Lingo.dev.", |
| 48 | + { |
| 49 | + text: Z.string(), |
| 50 | + targetLocale: Z.string().regex(/^[a-z]{2}(-[A-Z]{2})?$/), |
| 51 | + }, |
| 52 | + async ({ text, targetLocale }) => { |
| 53 | + const sourceLocale = await replexicaEngine.recognizeLocale(text); |
| 54 | + const data = await replexicaEngine.localizeText(text, { |
| 55 | + sourceLocale, |
| 56 | + targetLocale, |
| 57 | + }); |
| 58 | + return { content: [{ type: "text", text: data }] }; |
| 59 | + }, |
| 60 | + ); |
| 61 | + |
| 62 | + const transport = new StdioServerTransport(); |
| 63 | + await server.connect(transport); |
| 64 | + console.log("Lingo.dev MCP Server running on stdio"); |
| 65 | + }); |
0 commit comments