Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rude-crabs-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"lingo.dev": minor
---

add mcp command
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@inquirer/prompts": "^7.2.3",
"@lingo.dev/_sdk": "workspace:*",
"@lingo.dev/_spec": "workspace:*",
"@modelcontextprotocol/sdk": "^1.5.0",
"@paralleldrive/cuid2": "^2.2.2",
"chalk": "^5.4.1",
"cors": "^2.8.5",
Expand Down
65 changes: 65 additions & 0 deletions packages/cli/src/cli/cmd/mcp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Command } from "interactive-commander";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import Z from "zod";
import { ReplexicaEngine } from "@lingo.dev/_sdk";
import { getSettings } from "../utils/settings";
import { createAuthenticator } from "../utils/auth";

export default new Command()
.command("mcp")
.description("Use Lingo.dev model context provider with your AI agent")
.helpOption("-h, --help", "Show help")
.action(async (_, program) => {
const apiKey = program.args[0];
const settings = getSettings(apiKey);

if (!settings.auth.apiKey) {
console.error("No API key provided");
return;
}

const authenticator = createAuthenticator({
apiUrl: settings.auth.apiUrl,
apiKey: settings.auth.apiKey!,
});
const auth = await authenticator.whoami();

if (!auth) {
console.error("Not authenticated");
return;
} else {
console.log(`Authenticated as ${auth.email}`);
}

const replexicaEngine = new ReplexicaEngine({
apiKey: settings.auth.apiKey,
apiUrl: settings.auth.apiUrl,
});

const server = new McpServer({
name: "Lingo.dev",
version: "1.0.0",
});

server.tool(
"translate",
"Detect language and translate text with Lingo.dev.",
{
text: Z.string(),
targetLocale: Z.string().regex(/^[a-z]{2}(-[A-Z]{2})?$/),
},
async ({ text, targetLocale }) => {
const sourceLocale = await replexicaEngine.recognizeLocale(text);
const data = await replexicaEngine.localizeText(text, {
sourceLocale,
targetLocale,
});
return { content: [{ type: "text", text: data }] };
},
);

const transport = new StdioServerTransport();
await server.connect(transport);
console.log("Lingo.dev MCP Server running on stdio");
});
2 changes: 2 additions & 0 deletions packages/cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import configCmd from "./cmd/show";
import i18nCmd from "./cmd/i18n";
import lockfileCmd from "./cmd/lockfile";
import cleanupCmd from "./cmd/cleanup";
import mcpCmd from "./cmd/mcp";

import packageJson from "../../package.json";

Expand Down Expand Up @@ -40,6 +41,7 @@ Website: https://lingo.dev
.addCommand(configCmd)
.addCommand(lockfileCmd)
.addCommand(cleanupCmd)
.addCommand(mcpCmd)
.exitOverride((err) => {
// Exit with code 0 when help or version is displayed
if (err.code === "commander.helpDisplayed" || err.code === "commander.version" || err.code === "commander.help") {
Expand Down
49 changes: 49 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.