@evantahler/mcpx 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/.claude/settings.local.json +18 -0
  2. package/.claude/skills/mcpx.md +165 -0
  3. package/.claude/worktrees/elastic-jennings/.claude/settings.local.json +18 -0
  4. package/.claude/worktrees/elastic-jennings/.claude/skills/mcpcli.md +93 -0
  5. package/.claude/worktrees/elastic-jennings/.github/workflows/auto-release.yml +117 -0
  6. package/.claude/worktrees/elastic-jennings/.github/workflows/ci.yml +18 -0
  7. package/.claude/worktrees/elastic-jennings/.prettierignore +4 -0
  8. package/.claude/worktrees/elastic-jennings/.prettierrc +7 -0
  9. package/.claude/worktrees/elastic-jennings/CLAUDE.md +19 -0
  10. package/.claude/worktrees/elastic-jennings/LICENSE +21 -0
  11. package/.claude/worktrees/elastic-jennings/README.md +487 -0
  12. package/.claude/worktrees/elastic-jennings/bun.lock +381 -0
  13. package/.claude/worktrees/elastic-jennings/install.sh +55 -0
  14. package/.claude/worktrees/elastic-jennings/package.json +56 -0
  15. package/.claude/worktrees/elastic-jennings/src/cli.ts +39 -0
  16. package/.claude/worktrees/elastic-jennings/src/client/http.ts +100 -0
  17. package/.claude/worktrees/elastic-jennings/src/client/manager.ts +266 -0
  18. package/.claude/worktrees/elastic-jennings/src/client/oauth.ts +299 -0
  19. package/.claude/worktrees/elastic-jennings/src/client/stdio.ts +12 -0
  20. package/.claude/worktrees/elastic-jennings/src/commands/add.ts +155 -0
  21. package/.claude/worktrees/elastic-jennings/src/commands/auth.ts +114 -0
  22. package/.claude/worktrees/elastic-jennings/src/commands/exec.ts +91 -0
  23. package/.claude/worktrees/elastic-jennings/src/commands/index.ts +62 -0
  24. package/.claude/worktrees/elastic-jennings/src/commands/info.ts +38 -0
  25. package/.claude/worktrees/elastic-jennings/src/commands/list.ts +30 -0
  26. package/.claude/worktrees/elastic-jennings/src/commands/remove.ts +67 -0
  27. package/.claude/worktrees/elastic-jennings/src/commands/search.ts +45 -0
  28. package/.claude/worktrees/elastic-jennings/src/commands/skill.ts +70 -0
  29. package/.claude/worktrees/elastic-jennings/src/config/env.ts +41 -0
  30. package/.claude/worktrees/elastic-jennings/src/config/loader.ts +156 -0
  31. package/.claude/worktrees/elastic-jennings/src/config/schemas.ts +137 -0
  32. package/.claude/worktrees/elastic-jennings/src/context.ts +53 -0
  33. package/.claude/worktrees/elastic-jennings/src/output/formatter.ts +316 -0
  34. package/.claude/worktrees/elastic-jennings/src/output/logger.ts +114 -0
  35. package/.claude/worktrees/elastic-jennings/src/search/index.ts +69 -0
  36. package/.claude/worktrees/elastic-jennings/src/search/indexer.ts +92 -0
  37. package/.claude/worktrees/elastic-jennings/src/search/keyword.ts +86 -0
  38. package/.claude/worktrees/elastic-jennings/src/search/semantic.ts +75 -0
  39. package/.claude/worktrees/elastic-jennings/src/search/staleness.ts +8 -0
  40. package/.claude/worktrees/elastic-jennings/src/validation/schema.ts +77 -0
  41. package/.claude/worktrees/elastic-jennings/test/cli.test.ts +51 -0
  42. package/.claude/worktrees/elastic-jennings/test/client/manager.test.ts +249 -0
  43. package/.claude/worktrees/elastic-jennings/test/client/oauth.test.ts +328 -0
  44. package/.claude/worktrees/elastic-jennings/test/commands/add-remove.test.ts +253 -0
  45. package/.claude/worktrees/elastic-jennings/test/commands/exec.test.ts +105 -0
  46. package/.claude/worktrees/elastic-jennings/test/commands/info.test.ts +48 -0
  47. package/.claude/worktrees/elastic-jennings/test/commands/list.test.ts +39 -0
  48. package/.claude/worktrees/elastic-jennings/test/commands/skill.test.ts +98 -0
  49. package/.claude/worktrees/elastic-jennings/test/config/env.test.ts +61 -0
  50. package/.claude/worktrees/elastic-jennings/test/config/loader.test.ts +139 -0
  51. package/.claude/worktrees/elastic-jennings/test/fixtures/.keep +0 -0
  52. package/.claude/worktrees/elastic-jennings/test/fixtures/auth.json +10 -0
  53. package/.claude/worktrees/elastic-jennings/test/fixtures/mock-config/.keep +0 -0
  54. package/.claude/worktrees/elastic-jennings/test/fixtures/mock-config/servers.json +8 -0
  55. package/.claude/worktrees/elastic-jennings/test/fixtures/mock-server.ts +113 -0
  56. package/.claude/worktrees/elastic-jennings/test/fixtures/search.json +15 -0
  57. package/.claude/worktrees/elastic-jennings/test/fixtures/servers.json +18 -0
  58. package/.claude/worktrees/elastic-jennings/test/integration/stdio-server.test.ts +149 -0
  59. package/.claude/worktrees/elastic-jennings/test/output/formatter.test.ts +54 -0
  60. package/.claude/worktrees/elastic-jennings/test/output/logger.test.ts +89 -0
  61. package/.claude/worktrees/elastic-jennings/test/search/indexer.test.ts +32 -0
  62. package/.claude/worktrees/elastic-jennings/test/search/keyword.test.ts +80 -0
  63. package/.claude/worktrees/elastic-jennings/test/search/semantic.test.ts +32 -0
  64. package/.claude/worktrees/elastic-jennings/test/validation/schema.test.ts +113 -0
  65. package/.claude/worktrees/elastic-jennings/tsconfig.json +29 -0
  66. package/.cursor/rules/mcpx.mdc +165 -0
  67. package/LICENSE +21 -0
  68. package/README.md +627 -0
  69. package/package.json +58 -0
  70. package/src/cli.ts +72 -0
  71. package/src/client/browser.ts +24 -0
  72. package/src/client/debug-fetch.ts +81 -0
  73. package/src/client/elicitation.ts +368 -0
  74. package/src/client/http.ts +25 -0
  75. package/src/client/manager.ts +566 -0
  76. package/src/client/oauth.ts +314 -0
  77. package/src/client/sse.ts +17 -0
  78. package/src/client/stdio.ts +12 -0
  79. package/src/client/trace.ts +184 -0
  80. package/src/commands/add.ts +179 -0
  81. package/src/commands/auth.ts +114 -0
  82. package/src/commands/exec.ts +156 -0
  83. package/src/commands/index.ts +62 -0
  84. package/src/commands/info.ts +63 -0
  85. package/src/commands/list.ts +64 -0
  86. package/src/commands/ping.ts +69 -0
  87. package/src/commands/prompt.ts +60 -0
  88. package/src/commands/remove.ts +67 -0
  89. package/src/commands/resource.ts +46 -0
  90. package/src/commands/search.ts +49 -0
  91. package/src/commands/servers.ts +66 -0
  92. package/src/commands/skill.ts +112 -0
  93. package/src/commands/task.ts +82 -0
  94. package/src/config/env.ts +41 -0
  95. package/src/config/loader.ts +156 -0
  96. package/src/config/schemas.ts +152 -0
  97. package/src/context.ts +62 -0
  98. package/src/lib/input.ts +36 -0
  99. package/src/output/formatter.ts +884 -0
  100. package/src/output/logger.ts +173 -0
  101. package/src/search/index.ts +69 -0
  102. package/src/search/indexer.ts +92 -0
  103. package/src/search/keyword.ts +86 -0
  104. package/src/search/semantic.ts +75 -0
  105. package/src/search/staleness.ts +8 -0
  106. package/src/validation/schema.ts +103 -0
@@ -0,0 +1,156 @@
1
+ import { join, resolve } from "path";
2
+ import { homedir } from "os";
3
+ import { interpolateEnv } from "./env.ts";
4
+ import {
5
+ type Config,
6
+ type ServersFile,
7
+ type AuthFile,
8
+ type SearchIndex,
9
+ validateServersFile,
10
+ validateAuthFile,
11
+ validateSearchIndex,
12
+ } from "./schemas.ts";
13
+
14
+ const DEFAULT_CONFIG_DIR = join(homedir(), ".mcpx");
15
+
16
+ const EMPTY_SERVERS: ServersFile = { mcpServers: {} };
17
+ const EMPTY_AUTH: AuthFile = {};
18
+ const EMPTY_SEARCH_INDEX: SearchIndex = {
19
+ version: 1,
20
+ indexed_at: "",
21
+ embedding_model: "claude",
22
+ tools: [],
23
+ };
24
+
25
+ /** Read and parse a JSON file, returning undefined if it doesn't exist */
26
+ async function readJsonFile(path: string): Promise<unknown | undefined> {
27
+ const file = Bun.file(path);
28
+ if (!(await file.exists())) return undefined;
29
+ const text = await file.text();
30
+ return JSON.parse(text);
31
+ }
32
+
33
+ /** Resolve the config directory from options, env, cwd, or default */
34
+ function resolveConfigDir(configFlag?: string): string {
35
+ // 1. -c / --config flag
36
+ if (configFlag) return resolve(configFlag);
37
+
38
+ // 2. MCP_CONFIG_PATH env var
39
+ const envPath = process.env.MCP_CONFIG_PATH;
40
+ if (envPath) return resolve(envPath);
41
+
42
+ // 3. ./servers.json exists in cwd → use cwd
43
+ // (checked at load time, not here — we return the candidate dir)
44
+
45
+ // 4. Default ~/.mcpx/
46
+ return DEFAULT_CONFIG_DIR;
47
+ }
48
+
49
+ /** Check if servers.json exists in the given directory */
50
+ async function hasServersFile(dir: string): Promise<boolean> {
51
+ return Bun.file(join(dir, "servers.json")).exists();
52
+ }
53
+
54
+ export interface LoadConfigOptions {
55
+ configFlag?: string;
56
+ }
57
+
58
+ /** Load and validate all config files */
59
+ export async function loadConfig(options: LoadConfigOptions = {}): Promise<Config> {
60
+ // Resolve config directory
61
+ let configDir = resolveConfigDir(options.configFlag);
62
+
63
+ // If the resolved dir doesn't have servers.json, check cwd
64
+ if (!(await hasServersFile(configDir))) {
65
+ const cwd = process.cwd();
66
+ if (await hasServersFile(cwd)) {
67
+ configDir = cwd;
68
+ }
69
+ }
70
+
71
+ // Ensure config directory exists
72
+ await Bun.write(join(configDir, ".keep"), "").catch(() => {});
73
+ // Remove the .keep file, it was just to ensure the dir
74
+ Bun.file(join(configDir, ".keep"))
75
+ .exists()
76
+ .then((exists) => {
77
+ if (exists) Bun.write(join(configDir, ".keep"), "");
78
+ });
79
+
80
+ // Load servers.json
81
+ const serversPath = join(configDir, "servers.json");
82
+ const rawServers = await readJsonFile(serversPath);
83
+ let servers: ServersFile;
84
+ if (rawServers === undefined) {
85
+ servers = EMPTY_SERVERS;
86
+ } else {
87
+ servers = validateServersFile(rawServers);
88
+ // Interpolate env vars in server configs
89
+ servers = {
90
+ mcpServers: Object.fromEntries(
91
+ Object.entries(servers.mcpServers).map(([name, config]) => [name, interpolateEnv(config)]),
92
+ ),
93
+ };
94
+ }
95
+
96
+ // Load auth.json
97
+ const authPath = join(configDir, "auth.json");
98
+ const rawAuth = await readJsonFile(authPath);
99
+ const auth: AuthFile = rawAuth !== undefined ? validateAuthFile(rawAuth) : EMPTY_AUTH;
100
+
101
+ // Load search.json
102
+ const searchPath = join(configDir, "search.json");
103
+ const rawSearch = await readJsonFile(searchPath);
104
+ const searchIndex: SearchIndex =
105
+ rawSearch !== undefined ? validateSearchIndex(rawSearch) : EMPTY_SEARCH_INDEX;
106
+
107
+ return { configDir, servers, auth, searchIndex };
108
+ }
109
+
110
+ /** Save auth.json to the config directory */
111
+ export async function saveAuth(configDir: string, auth: AuthFile): Promise<void> {
112
+ await Bun.write(join(configDir, "auth.json"), JSON.stringify(auth, null, 2) + "\n");
113
+ }
114
+
115
+ /** Load search.json from the config directory */
116
+ export async function loadSearchIndex(configDir: string): Promise<SearchIndex> {
117
+ const raw = await readJsonFile(join(configDir, "search.json"));
118
+ return raw !== undefined ? validateSearchIndex(raw) : { ...EMPTY_SEARCH_INDEX };
119
+ }
120
+
121
+ /** Save search.json to the config directory */
122
+ export async function saveSearchIndex(configDir: string, index: SearchIndex): Promise<void> {
123
+ await Bun.write(join(configDir, "search.json"), JSON.stringify(index, null, 2) + "\n");
124
+ }
125
+
126
+ /** Save servers.json to the config directory */
127
+ export async function saveServers(configDir: string, servers: ServersFile): Promise<void> {
128
+ await Bun.write(join(configDir, "servers.json"), JSON.stringify(servers, null, 2) + "\n");
129
+ }
130
+
131
+ /** Load servers.json without env interpolation (preserves ${VAR} placeholders) */
132
+ export async function loadRawServers(
133
+ configFlag?: string,
134
+ ): Promise<{ configDir: string; servers: ServersFile }> {
135
+ let configDir = resolveConfigDir(configFlag);
136
+
137
+ if (!(await hasServersFile(configDir))) {
138
+ const cwd = process.cwd();
139
+ if (await hasServersFile(cwd)) {
140
+ configDir = cwd;
141
+ }
142
+ }
143
+
144
+ const serversPath = join(configDir, "servers.json");
145
+ const raw = await readJsonFile(serversPath);
146
+ const servers = raw !== undefined ? validateServersFile(raw) : EMPTY_SERVERS;
147
+
148
+ return { configDir, servers };
149
+ }
150
+
151
+ /** Load auth.json without loading the full config */
152
+ export async function loadRawAuth(configDir: string): Promise<AuthFile> {
153
+ const authPath = join(configDir, "auth.json");
154
+ const raw = await readJsonFile(authPath);
155
+ return raw !== undefined ? validateAuthFile(raw) : EMPTY_AUTH;
156
+ }
@@ -0,0 +1,152 @@
1
+ import type { Tool, Resource, Prompt } from "@modelcontextprotocol/sdk/types.js";
2
+ import type {
3
+ OAuthTokens,
4
+ OAuthClientInformation,
5
+ OAuthClientInformationMixed,
6
+ } from "@modelcontextprotocol/sdk/shared/auth.js";
7
+
8
+ // Re-export SDK types we use throughout the codebase
9
+ export type {
10
+ Tool,
11
+ Resource,
12
+ Prompt,
13
+ OAuthTokens,
14
+ OAuthClientInformation,
15
+ OAuthClientInformationMixed,
16
+ };
17
+
18
+ // --- Server config (our format, not MCP spec) ---
19
+
20
+ /** Stdio MCP server config */
21
+ export interface StdioServerConfig {
22
+ command: string;
23
+ args?: string[];
24
+ env?: Record<string, string>;
25
+ cwd?: string;
26
+ allowedTools?: string[];
27
+ disabledTools?: string[];
28
+ }
29
+
30
+ /** HTTP MCP server config */
31
+ export interface HttpServerConfig {
32
+ url: string;
33
+ headers?: Record<string, string>;
34
+ transport?: "sse" | "streamable-http";
35
+ allowedTools?: string[];
36
+ disabledTools?: string[];
37
+ }
38
+
39
+ export type ServerConfig = StdioServerConfig | HttpServerConfig;
40
+
41
+ export function isStdioServer(config: ServerConfig): config is StdioServerConfig {
42
+ return "command" in config;
43
+ }
44
+
45
+ export function isHttpServer(config: ServerConfig): config is HttpServerConfig {
46
+ return "url" in config;
47
+ }
48
+
49
+ /** Top-level servers.json shape */
50
+ export interface ServersFile {
51
+ mcpServers: Record<string, ServerConfig>;
52
+ }
53
+
54
+ // --- Auth storage (wraps SDK's OAuthTokens with our persistence fields) ---
55
+
56
+ /** Per-server auth entry stored in auth.json */
57
+ export interface AuthEntry {
58
+ tokens: OAuthTokens;
59
+ expires_at?: string;
60
+ client_info?: OAuthClientInformationMixed;
61
+ complete?: boolean;
62
+ }
63
+
64
+ /** Top-level auth.json shape */
65
+ export type AuthFile = Record<string, AuthEntry>;
66
+
67
+ // --- Search index (entirely our format) ---
68
+
69
+ /** A single tool entry in the search index */
70
+ export interface IndexedTool {
71
+ server: string;
72
+ tool: string;
73
+ description: string;
74
+ input_schema?: Tool["inputSchema"];
75
+ scenarios: string[];
76
+ keywords: string[];
77
+ embedding: number[];
78
+ }
79
+
80
+ /** Top-level search.json shape */
81
+ export interface SearchIndex {
82
+ version: number;
83
+ indexed_at: string;
84
+ embedding_model: string;
85
+ tools: IndexedTool[];
86
+ }
87
+
88
+ // --- Combined config ---
89
+
90
+ /** Validated config returned by loadConfig */
91
+ export interface Config {
92
+ configDir: string;
93
+ servers: ServersFile;
94
+ auth: AuthFile;
95
+ searchIndex: SearchIndex;
96
+ }
97
+
98
+ // --- Validation ---
99
+
100
+ /** Validate that a parsed object looks like a valid servers.json */
101
+ export function validateServersFile(data: unknown): ServersFile {
102
+ if (typeof data !== "object" || data === null) {
103
+ throw new Error("servers.json must be a JSON object");
104
+ }
105
+
106
+ const obj = data as Record<string, unknown>;
107
+ if (typeof obj.mcpServers !== "object" || obj.mcpServers === null) {
108
+ throw new Error('servers.json must have a "mcpServers" object');
109
+ }
110
+
111
+ const servers = obj.mcpServers as Record<string, unknown>;
112
+ for (const [name, config] of Object.entries(servers)) {
113
+ if (typeof config !== "object" || config === null) {
114
+ throw new Error(`Server "${name}" must be an object`);
115
+ }
116
+ const c = config as Record<string, unknown>;
117
+ const hasCommand = typeof c.command === "string";
118
+ const hasUrl = typeof c.url === "string";
119
+ if (!hasCommand && !hasUrl) {
120
+ throw new Error(`Server "${name}" must have either "command" (stdio) or "url" (http)`);
121
+ }
122
+ if (hasUrl && c.transport !== undefined) {
123
+ if (c.transport !== "sse" && c.transport !== "streamable-http") {
124
+ throw new Error(
125
+ `Server "${name}" has invalid transport "${c.transport}" — must be "sse" or "streamable-http"`,
126
+ );
127
+ }
128
+ }
129
+ }
130
+
131
+ return data as ServersFile;
132
+ }
133
+
134
+ /** Validate auth.json — lenient, just check shape */
135
+ export function validateAuthFile(data: unknown): AuthFile {
136
+ if (typeof data !== "object" || data === null) {
137
+ throw new Error("auth.json must be a JSON object");
138
+ }
139
+ return data as AuthFile;
140
+ }
141
+
142
+ /** Validate search.json — lenient, just check shape */
143
+ export function validateSearchIndex(data: unknown): SearchIndex {
144
+ if (typeof data !== "object" || data === null) {
145
+ throw new Error("search.json must be a JSON object");
146
+ }
147
+ const obj = data as Record<string, unknown>;
148
+ if (!Array.isArray(obj.tools)) {
149
+ throw new Error('search.json must have a "tools" array');
150
+ }
151
+ return data as SearchIndex;
152
+ }
package/src/context.ts ADDED
@@ -0,0 +1,62 @@
1
+ import type { Command } from "commander";
2
+ import { loadConfig, type LoadConfigOptions } from "./config/loader.ts";
3
+ import { ServerManager } from "./client/manager.ts";
4
+ import type { Config } from "./config/schemas.ts";
5
+ import type { FormatOptions } from "./output/formatter.ts";
6
+ import { logger } from "./output/logger.ts";
7
+
8
+ export interface AppContext {
9
+ config: Config;
10
+ manager: ServerManager;
11
+ formatOptions: FormatOptions;
12
+ }
13
+
14
+ /** Build the app context from the root commander program options */
15
+ export async function getContext(program: Command): Promise<AppContext> {
16
+ const opts = program.opts();
17
+
18
+ const config = await loadConfig({
19
+ configFlag: opts.config as string | undefined,
20
+ });
21
+
22
+ const verbose = !!(
23
+ (opts.verbose as boolean | undefined) ||
24
+ process.env.MCP_DEBUG === "1" ||
25
+ process.env.MCP_DEBUG === "true"
26
+ );
27
+ const showSecrets = !!(opts.showSecrets as boolean | undefined);
28
+ const concurrency = Number(process.env.MCP_CONCURRENCY ?? 5);
29
+ const timeout = Number(process.env.MCP_TIMEOUT ?? 1800) * 1000;
30
+ const maxRetries = Number(process.env.MCP_MAX_RETRIES ?? 3);
31
+ const logLevel = (opts.logLevel as string | undefined) ?? "warning";
32
+
33
+ const json = !!(opts.json as boolean | undefined);
34
+ // Commander's --no-interactive sets opts.interactive = false (default true)
35
+ const noInteractive = opts.interactive === false;
36
+
37
+ const manager = new ServerManager({
38
+ servers: config.servers,
39
+ configDir: config.configDir,
40
+ auth: config.auth,
41
+ concurrency,
42
+ verbose,
43
+ showSecrets,
44
+ timeout,
45
+ maxRetries,
46
+ logLevel,
47
+ json,
48
+ noInteractive,
49
+ });
50
+
51
+ const formatOptions: FormatOptions = {
52
+ json: opts.json as boolean | undefined,
53
+ withDescriptions: opts.withDescriptions as boolean | undefined,
54
+ verbose,
55
+ showSecrets,
56
+ logLevel,
57
+ };
58
+
59
+ logger.configure(formatOptions);
60
+
61
+ return { config, manager, formatOptions };
62
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Shared helpers for parsing JSON arguments and reading from stdin.
3
+ */
4
+
5
+ /** Parse a JSON string as a key-value object, optionally coercing all values to strings. */
6
+ export function parseJsonArgs(
7
+ str: string,
8
+ opts?: { coerceToString?: boolean },
9
+ ): Record<string, unknown> {
10
+ try {
11
+ const parsed = JSON.parse(str);
12
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
13
+ throw new Error("Arguments must be a JSON object");
14
+ }
15
+ if (opts?.coerceToString) {
16
+ return Object.fromEntries(Object.entries(parsed).map(([k, v]) => [k, String(v)]));
17
+ }
18
+ return parsed as Record<string, unknown>;
19
+ } catch (err) {
20
+ if (err instanceof SyntaxError) {
21
+ throw new Error(`Invalid JSON: ${err.message}`);
22
+ }
23
+ throw err;
24
+ }
25
+ }
26
+
27
+ /** Read all data from stdin until EOF. */
28
+ export async function readStdin(): Promise<string> {
29
+ const chunks: string[] = [];
30
+ const reader = process.stdin;
31
+ reader.setEncoding("utf-8");
32
+ for await (const chunk of reader) {
33
+ chunks.push(chunk as string);
34
+ }
35
+ return chunks.join("");
36
+ }