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
66 changes: 3 additions & 63 deletions packages/cli/commands/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { mkdir, readFile, writeFile } from "node:fs/promises";
import { dirname, join, relative } from "node:path";
import ora, { type Ora } from "ora";

import { App, listApps } from "../services/bootstrap.js";
import {
ConfigPaths,
getServersConfig,
Expand All @@ -17,21 +16,15 @@ import {
import { configStore } from "../stores/config.js";
import { handleError } from "../utils/errors.js";
import { fileExists } from "../utils/file.js";
import {
appIdOption,
configScopeOption,
editorOption,
} from "../utils/options.js";
import { configScopeOption, editorOption } from "../utils/options.js";

export const mcpAction = async (options: {
editor?: SupportedEditor;
appId?: string;
scope?: "local" | "global";
}) => {
const config = configStore.getConfig();
let spinner: Ora | undefined;
let selectedEditor = options.editor;
let selectedApp: App | undefined;

// Select Editor/Client
if (!selectedEditor) {
Expand All @@ -44,45 +37,6 @@ export const mcpAction = async (options: {
});
}

// Select App ID
try {
spinner = ora(`Loading apps from ${chalk.cyan(config.baseUrl)}...`).start();
const apps = listApps();
spinner.succeed(`Loaded apps from ${chalk.cyan(config.baseUrl)}.`);

if (apps.length === 0) {
throw new Error("You don't have any apps yet. Please create one.");
}

let selectedAppId: string;
if (options.appId) {
// If appId is provided, try to find it directly
const app = apps.find(({ id }) => id === options.appId);
if (!app) {
throw new Error(`Could not find app with ID: ${options.appId}`);
}

selectedAppId = app.id;
} else {
// Otherwise, show selection prompt
const nonDemoApp = apps.find((app) => !app.demo);
const longestName = Math.max(...apps.map((app) => app.name.length));

selectedAppId = await select({
message: "Select an app",
default: config.appId ?? nonDemoApp?.id,
choices: apps.map((app) => ({
name: `${app.name.padEnd(longestName, " ")}${app.demo ? " [Demo]" : ""}`,
value: app.id,
})),
});
}
selectedApp = apps.find((app) => app.id === selectedAppId)!;
} catch (error) {
spinner?.fail("Loading apps failed.");
handleError(error, "MCP Configuration");
}

// Determine Config Path
const projectPath = configStore.getProjectPath();
const globalPath = resolveConfigPath(selectedEditor, false);
Expand Down Expand Up @@ -152,7 +106,7 @@ export const mcpAction = async (options: {

// Prompt for Add/Update
let targetEntryKey: string;
const defaultNewKey = `Reflag - ${selectedApp.name}`;
const defaultNewKey = `Reflag`;

if (existingReflagEntries.length === 0) {
targetEntryKey = defaultNewKey;
Expand Down Expand Up @@ -183,13 +137,8 @@ export const mcpAction = async (options: {
}

// Construct the MCP endpoint URL
const mcpUrlBase = config.apiUrl + "/mcp";
const mcpUrlWithAppId = `${mcpUrlBase}?appId=${selectedApp.id}`;

const newEntryValue = {
type: "stdio",
command: "npx",
args: ["mcp-remote@latest", mcpUrlWithAppId],
url: config.apiUrl + "/mcp",
};

// Update Config Object
Expand Down Expand Up @@ -228,16 +177,7 @@ export function registerMcpCommand(cli: Command) {
cli
.command("mcp")
.description("Configure Reflag's remote MCP server for your AI assistant.")
.addOption(appIdOption)
.addOption(editorOption)
.addOption(configScopeOption)
.action(mcpAction);

// Update the config with the cli override values
cli.hook("preAction", (_, command) => {
const { appId } = command.opts();
configStore.setConfig({
appId,
});
});
}
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reflag/cli",
"version": "1.0.1",
"version": "1.0.2",
"packageManager": "yarn@4.1.1",
"description": "CLI for Reflag service",
"main": "./dist/index.js",
Expand Down