@almyty/mcp-server 1.0.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.
- package/README.md +131 -0
- package/dist/auth.d.ts +32 -0
- package/dist/auth.js +93 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +271 -0
- package/dist/proxy.d.ts +31 -0
- package/dist/proxy.js +116 -0
- package/package.json +42 -0
package/README.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# @almyty/mcp-server
|
|
2
|
+
|
|
3
|
+
Skill-first API proxy for any LLM — turns APIs into AI skills with minimal token overhead.
|
|
4
|
+
|
|
5
|
+
## The Problem with Traditional MCP
|
|
6
|
+
|
|
7
|
+
MCP dumps every tool's full JSON Schema into the LLM's context window:
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
20 tools × ~200 tokens/schema = ~4,000 tokens/turn (always in context)
|
|
11
|
+
400 tools = 80,000+ tokens (unusable)
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## The Skill-First Solution
|
|
15
|
+
|
|
16
|
+
Instead of N tool schemas, inject:
|
|
17
|
+
1. **2 tools** (`almyty_execute` + `almyty_search`) = ~300 tokens base
|
|
18
|
+
2. **Skills as prompts** = loaded on-demand, only when needed
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
Token overhead: ~300 tokens (fixed) vs ~4,000+ tokens (scales with tool count)
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Skills are compact markdown that teach the LLM **workflows** — when to use a tool, what parameters to collect, how to handle errors. The LLM loads a skill on demand, then calls `almyty_execute` with the right tool name.
|
|
25
|
+
|
|
26
|
+
## How It Works
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
LLM wants to "create a pet"
|
|
30
|
+
↓
|
|
31
|
+
1. Calls almyty_search("pet") → finds relevant tools
|
|
32
|
+
2. Loads skill-petstore prompt → learns the workflow
|
|
33
|
+
3. Calls almyty_execute(tool_name="addPet", parameters={...})
|
|
34
|
+
↓
|
|
35
|
+
almyty backend executes against the actual API
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Quick Start
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
export APIFAI_URL=http://localhost:4000
|
|
42
|
+
export APIFAI_TOKEN=your-jwt-token
|
|
43
|
+
npx @almyty/mcp-server
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Or login interactively:
|
|
47
|
+
```bash
|
|
48
|
+
npx @almyty/mcp-server login
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Configuration
|
|
52
|
+
|
|
53
|
+
### Claude Code
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
claude mcp add almyty -- npx -y @almyty/mcp-server
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Cursor (`.cursor/mcp.json`)
|
|
60
|
+
|
|
61
|
+
```json
|
|
62
|
+
{
|
|
63
|
+
"mcpServers": {
|
|
64
|
+
"almyty": {
|
|
65
|
+
"command": "npx",
|
|
66
|
+
"args": ["-y", "@almyty/mcp-server"],
|
|
67
|
+
"env": { "APIFAI_URL": "http://localhost:4000", "APIFAI_TOKEN": "..." }
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### OpenAI Codex CLI (`~/.codex/config.toml`)
|
|
74
|
+
|
|
75
|
+
```toml
|
|
76
|
+
[mcp_servers.almyty]
|
|
77
|
+
command = "npx"
|
|
78
|
+
args = ["-y", "@almyty/mcp-server"]
|
|
79
|
+
|
|
80
|
+
[mcp_servers.almyty.env]
|
|
81
|
+
APIFAI_URL = "http://localhost:4000"
|
|
82
|
+
APIFAI_TOKEN = "your-jwt-token"
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### GitHub Copilot (`.vscode/mcp.json`)
|
|
86
|
+
|
|
87
|
+
```json
|
|
88
|
+
{ "servers": { "almyty": { "command": "npx", "args": ["-y", "@almyty/mcp-server"] } } }
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Google Gemini CLI (`~/.gemini/settings.json`)
|
|
92
|
+
|
|
93
|
+
```json
|
|
94
|
+
{ "mcpServers": { "almyty": { "command": "npx", "args": ["-y", "@almyty/mcp-server"] } } }
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Environment Variables
|
|
98
|
+
|
|
99
|
+
| Variable | Default | Description |
|
|
100
|
+
|----------|---------|-------------|
|
|
101
|
+
| `APIFAI_URL` | `http://localhost:4000` | almyty backend URL |
|
|
102
|
+
| `APIFAI_TOKEN` | — | JWT Bearer token |
|
|
103
|
+
| `APIFAI_GATEWAY_ID` | — | Scope to a specific gateway |
|
|
104
|
+
| `APIFAI_MODE` | `skill-first` | `skill-first` or `full` |
|
|
105
|
+
|
|
106
|
+
## Modes
|
|
107
|
+
|
|
108
|
+
| Mode | Tools in context | Token overhead | How it works |
|
|
109
|
+
|------|-----------------|----------------|--------------|
|
|
110
|
+
| `skill-first` | 2 (execute + search) | ~300 tokens | Skills loaded on demand via prompts |
|
|
111
|
+
| `full` | All N tools | ~N×200 tokens | Traditional MCP (every schema in context) |
|
|
112
|
+
|
|
113
|
+
## Architecture
|
|
114
|
+
|
|
115
|
+
```
|
|
116
|
+
Your LLM (Claude, Cursor, Copilot, Codex, Gemini)
|
|
117
|
+
↕ MCP stdio (2 tools + skills as prompts)
|
|
118
|
+
@almyty/mcp-server
|
|
119
|
+
↕ HTTP/JSON-RPC
|
|
120
|
+
almyty backend (tools + skills + auth)
|
|
121
|
+
↕ HTTP/GraphQL/SOAP/gRPC
|
|
122
|
+
Your APIs (OpenAPI, GraphQL, SOAP, Protobuf)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
**Auth model:**
|
|
126
|
+
- User → almyty: JWT token (env var or `npx @almyty/mcp-server login`)
|
|
127
|
+
- almyty → APIs: Managed within almyty (API keys, OAuth, etc.)
|
|
128
|
+
|
|
129
|
+
## License
|
|
130
|
+
|
|
131
|
+
BSL-1.1
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authentication helpers for the almyty MCP server.
|
|
3
|
+
*
|
|
4
|
+
* Supports:
|
|
5
|
+
* 1. Environment variable (APIFAI_TOKEN) — for CI/scripts
|
|
6
|
+
* 2. Stored credentials (~/.almyty/credentials.json) — for interactive use
|
|
7
|
+
* 3. Interactive login (npx @almyty/mcp-server login) — stores credentials
|
|
8
|
+
*/
|
|
9
|
+
interface StoredCredentials {
|
|
10
|
+
url: string;
|
|
11
|
+
token: string;
|
|
12
|
+
email?: string;
|
|
13
|
+
expiresAt?: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Load stored credentials from ~/.almyty/credentials.json
|
|
17
|
+
*/
|
|
18
|
+
export declare function loadCredentials(): StoredCredentials | null;
|
|
19
|
+
/**
|
|
20
|
+
* Save credentials to ~/.almyty/credentials.json
|
|
21
|
+
*/
|
|
22
|
+
export declare function saveCredentials(creds: StoredCredentials): void;
|
|
23
|
+
/**
|
|
24
|
+
* Remove stored credentials
|
|
25
|
+
*/
|
|
26
|
+
export declare function logout(): void;
|
|
27
|
+
/**
|
|
28
|
+
* Interactive login: prompts for email/password, authenticates against
|
|
29
|
+
* the almyty backend, and stores the JWT token.
|
|
30
|
+
*/
|
|
31
|
+
export declare function login(baseUrl: string): Promise<void>;
|
|
32
|
+
export {};
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authentication helpers for the almyty MCP server.
|
|
3
|
+
*
|
|
4
|
+
* Supports:
|
|
5
|
+
* 1. Environment variable (APIFAI_TOKEN) — for CI/scripts
|
|
6
|
+
* 2. Stored credentials (~/.almyty/credentials.json) — for interactive use
|
|
7
|
+
* 3. Interactive login (npx @almyty/mcp-server login) — stores credentials
|
|
8
|
+
*/
|
|
9
|
+
import { readFileSync, writeFileSync, mkdirSync, unlinkSync, existsSync } from 'fs';
|
|
10
|
+
import { homedir } from 'os';
|
|
11
|
+
import { join } from 'path';
|
|
12
|
+
import { createInterface } from 'readline';
|
|
13
|
+
const CREDENTIALS_DIR = join(homedir(), '.almyty');
|
|
14
|
+
const CREDENTIALS_FILE = join(CREDENTIALS_DIR, 'credentials.json');
|
|
15
|
+
/**
|
|
16
|
+
* Load stored credentials from ~/.almyty/credentials.json
|
|
17
|
+
*/
|
|
18
|
+
export function loadCredentials() {
|
|
19
|
+
try {
|
|
20
|
+
if (!existsSync(CREDENTIALS_FILE))
|
|
21
|
+
return null;
|
|
22
|
+
const data = readFileSync(CREDENTIALS_FILE, 'utf-8');
|
|
23
|
+
return JSON.parse(data);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Save credentials to ~/.almyty/credentials.json
|
|
31
|
+
*/
|
|
32
|
+
export function saveCredentials(creds) {
|
|
33
|
+
mkdirSync(CREDENTIALS_DIR, { recursive: true });
|
|
34
|
+
writeFileSync(CREDENTIALS_FILE, JSON.stringify(creds, null, 2), { mode: 0o600 });
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Remove stored credentials
|
|
38
|
+
*/
|
|
39
|
+
export function logout() {
|
|
40
|
+
try {
|
|
41
|
+
if (existsSync(CREDENTIALS_FILE)) {
|
|
42
|
+
unlinkSync(CREDENTIALS_FILE);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
// Ignore errors
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Interactive login: prompts for email/password, authenticates against
|
|
51
|
+
* the almyty backend, and stores the JWT token.
|
|
52
|
+
*/
|
|
53
|
+
export async function login(baseUrl) {
|
|
54
|
+
const rl = createInterface({
|
|
55
|
+
input: process.stdin,
|
|
56
|
+
output: process.stderr, // Use stderr so stdout stays clean for MCP
|
|
57
|
+
});
|
|
58
|
+
const ask = (question) => new Promise((resolve) => rl.question(question, resolve));
|
|
59
|
+
try {
|
|
60
|
+
console.error(`Logging in to ${baseUrl}...\n`);
|
|
61
|
+
const email = await ask('Email: ');
|
|
62
|
+
const password = await ask('Password: ');
|
|
63
|
+
console.error('\nAuthenticating...');
|
|
64
|
+
const response = await fetch(`${baseUrl}/api/auth/login`, {
|
|
65
|
+
method: 'POST',
|
|
66
|
+
headers: { 'Content-Type': 'application/json' },
|
|
67
|
+
body: JSON.stringify({ email, password }),
|
|
68
|
+
});
|
|
69
|
+
if (!response.ok) {
|
|
70
|
+
const error = await response.json().catch(() => ({ message: 'Authentication failed' }));
|
|
71
|
+
console.error(`Error: ${error.message || 'Authentication failed'}`);
|
|
72
|
+
process.exit(1);
|
|
73
|
+
}
|
|
74
|
+
const data = await response.json();
|
|
75
|
+
const token = data.accessToken || data.token;
|
|
76
|
+
if (!token) {
|
|
77
|
+
console.error('Error: No token received from server');
|
|
78
|
+
process.exit(1);
|
|
79
|
+
}
|
|
80
|
+
saveCredentials({
|
|
81
|
+
url: baseUrl,
|
|
82
|
+
token,
|
|
83
|
+
email,
|
|
84
|
+
});
|
|
85
|
+
console.error(`\nLogged in successfully as ${email}`);
|
|
86
|
+
console.error(`Credentials saved to ${CREDENTIALS_FILE}`);
|
|
87
|
+
console.error('\nYou can now use the MCP server without APIFAI_TOKEN:');
|
|
88
|
+
console.error(' npx @almyty/mcp-server');
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
rl.close();
|
|
92
|
+
}
|
|
93
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* @almyty/mcp-server — Skill-first API proxy for any LLM
|
|
4
|
+
*
|
|
5
|
+
* Instead of dumping N tool schemas into context (N * ~200 tokens each),
|
|
6
|
+
* this injects:
|
|
7
|
+
* 1. Skills (compact markdown prompts) — the LLM's knowledge layer
|
|
8
|
+
* 2. ONE universal executor tool — handles all API calls
|
|
9
|
+
* 3. ONE search tool — finds the right skill/tool on demand
|
|
10
|
+
*
|
|
11
|
+
* Token comparison:
|
|
12
|
+
* Traditional MCP: 20 tools = ~4,000 tokens always in context
|
|
13
|
+
* Skill-first: 2 tools + skills on-demand = ~300 tokens base
|
|
14
|
+
*
|
|
15
|
+
* The LLM reads a skill to understand a workflow, then calls almyty_execute
|
|
16
|
+
* with the tool name and parameters. Skills are loaded on demand via
|
|
17
|
+
* MCP prompts — only when the LLM actually needs them.
|
|
18
|
+
*
|
|
19
|
+
* Works with every major AI coding assistant:
|
|
20
|
+
* - Claude Code (.mcp.json / .claude/skills/)
|
|
21
|
+
* - Cursor (.cursor/mcp.json)
|
|
22
|
+
* - Windsurf (~/.codeium/windsurf/mcp_config.json)
|
|
23
|
+
* - OpenAI Codex CLI (~/.codex/config.toml)
|
|
24
|
+
* - GitHub Copilot (.vscode/mcp.json)
|
|
25
|
+
* - Google Gemini CLI (~/.gemini/settings.json)
|
|
26
|
+
*
|
|
27
|
+
* Environment variables:
|
|
28
|
+
* APIFAI_URL - Base URL of the almyty backend (default: http://localhost:4000)
|
|
29
|
+
* APIFAI_TOKEN - JWT Bearer token for authentication
|
|
30
|
+
* APIFAI_GATEWAY_ID - Optional: scope to a specific gateway
|
|
31
|
+
* APIFAI_MODE - "skill-first" (default) | "full" (registers all tools individually)
|
|
32
|
+
*/
|
|
33
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* @almyty/mcp-server — Skill-first API proxy for any LLM
|
|
4
|
+
*
|
|
5
|
+
* Instead of dumping N tool schemas into context (N * ~200 tokens each),
|
|
6
|
+
* this injects:
|
|
7
|
+
* 1. Skills (compact markdown prompts) — the LLM's knowledge layer
|
|
8
|
+
* 2. ONE universal executor tool — handles all API calls
|
|
9
|
+
* 3. ONE search tool — finds the right skill/tool on demand
|
|
10
|
+
*
|
|
11
|
+
* Token comparison:
|
|
12
|
+
* Traditional MCP: 20 tools = ~4,000 tokens always in context
|
|
13
|
+
* Skill-first: 2 tools + skills on-demand = ~300 tokens base
|
|
14
|
+
*
|
|
15
|
+
* The LLM reads a skill to understand a workflow, then calls almyty_execute
|
|
16
|
+
* with the tool name and parameters. Skills are loaded on demand via
|
|
17
|
+
* MCP prompts — only when the LLM actually needs them.
|
|
18
|
+
*
|
|
19
|
+
* Works with every major AI coding assistant:
|
|
20
|
+
* - Claude Code (.mcp.json / .claude/skills/)
|
|
21
|
+
* - Cursor (.cursor/mcp.json)
|
|
22
|
+
* - Windsurf (~/.codeium/windsurf/mcp_config.json)
|
|
23
|
+
* - OpenAI Codex CLI (~/.codex/config.toml)
|
|
24
|
+
* - GitHub Copilot (.vscode/mcp.json)
|
|
25
|
+
* - Google Gemini CLI (~/.gemini/settings.json)
|
|
26
|
+
*
|
|
27
|
+
* Environment variables:
|
|
28
|
+
* APIFAI_URL - Base URL of the almyty backend (default: http://localhost:4000)
|
|
29
|
+
* APIFAI_TOKEN - JWT Bearer token for authentication
|
|
30
|
+
* APIFAI_GATEWAY_ID - Optional: scope to a specific gateway
|
|
31
|
+
* APIFAI_MODE - "skill-first" (default) | "full" (registers all tools individually)
|
|
32
|
+
*/
|
|
33
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
34
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
35
|
+
import { z } from 'zod';
|
|
36
|
+
import { loadCredentials } from './auth.js';
|
|
37
|
+
import { AlmytyProxy } from './proxy.js';
|
|
38
|
+
const APIFAI_URL = process.env.APIFAI_URL || 'http://localhost:4000';
|
|
39
|
+
const APIFAI_GATEWAY_ID = process.env.APIFAI_GATEWAY_ID;
|
|
40
|
+
const APIFAI_MODE = (process.env.APIFAI_MODE || 'skill-first');
|
|
41
|
+
async function main() {
|
|
42
|
+
// Resolve token: env var > stored credentials
|
|
43
|
+
let token = process.env.APIFAI_TOKEN;
|
|
44
|
+
if (!token) {
|
|
45
|
+
const creds = loadCredentials();
|
|
46
|
+
token = creds?.token;
|
|
47
|
+
}
|
|
48
|
+
if (!token) {
|
|
49
|
+
console.error('Error: No authentication token found.\n' +
|
|
50
|
+
'Set APIFAI_TOKEN environment variable or run: npx @almyty/mcp-server login');
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
const proxy = new AlmytyProxy(APIFAI_URL, token, APIFAI_GATEWAY_ID);
|
|
54
|
+
// Fetch tools + skills from backend
|
|
55
|
+
const [tools, skills] = await Promise.all([
|
|
56
|
+
proxy.fetchTools(),
|
|
57
|
+
proxy.fetchSkills(),
|
|
58
|
+
]);
|
|
59
|
+
console.error(`almyty: ${tools.length} tools, ${skills.length} skills (mode: ${APIFAI_MODE})`);
|
|
60
|
+
const server = new McpServer({
|
|
61
|
+
name: 'almyty',
|
|
62
|
+
version: '1.0.0',
|
|
63
|
+
});
|
|
64
|
+
if (APIFAI_MODE === 'skill-first') {
|
|
65
|
+
// =====================================================
|
|
66
|
+
// SKILL-FIRST MODE (default) — minimal token overhead
|
|
67
|
+
// =====================================================
|
|
68
|
+
// Register only 2 tools instead of N:
|
|
69
|
+
// 1. almyty_execute — universal tool executor
|
|
70
|
+
// 2. almyty_search — find tools by query
|
|
71
|
+
//
|
|
72
|
+
// Skills are registered as prompts (loaded on-demand).
|
|
73
|
+
// The LLM reads a skill, learns the workflow, then calls
|
|
74
|
+
// almyty_execute with the right tool name + params.
|
|
75
|
+
// =====================================================
|
|
76
|
+
// Build a compact tool index for the search tool
|
|
77
|
+
const toolIndex = tools.map(t => ({
|
|
78
|
+
name: t.name,
|
|
79
|
+
description: t.description || '',
|
|
80
|
+
}));
|
|
81
|
+
// --- Universal executor ---
|
|
82
|
+
// ONE tool that can call ANY almyty tool by name.
|
|
83
|
+
// ~150 tokens in context instead of N * ~200 tokens.
|
|
84
|
+
server.tool('almyty_execute', 'Execute any almyty API tool by name. Read the relevant skill prompt first to understand which tool to use and what parameters are needed.', {
|
|
85
|
+
tool_name: z.string().describe('Name of the almyty tool to execute (from skill instructions)'),
|
|
86
|
+
parameters: z.record(z.unknown()).describe('Parameters for the tool (see skill for required params)'),
|
|
87
|
+
}, async (args) => {
|
|
88
|
+
try {
|
|
89
|
+
const result = await proxy.callTool(args.tool_name, args.parameters);
|
|
90
|
+
return {
|
|
91
|
+
content: [{
|
|
92
|
+
type: 'text',
|
|
93
|
+
text: typeof result === 'string' ? result : JSON.stringify(result, null, 2),
|
|
94
|
+
}],
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
return {
|
|
99
|
+
content: [{ type: 'text', text: `Error: ${error.message}` }],
|
|
100
|
+
isError: true,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
// --- Search tool ---
|
|
105
|
+
// Helps the LLM find the right tool/skill without having all schemas in context.
|
|
106
|
+
server.tool('almyty_search', 'Search available API tools by keyword. Returns matching tool names and descriptions. Use this to discover which tools are available before calling almyty_execute.', {
|
|
107
|
+
query: z.string().describe('Search query (e.g., "create pet", "list users", "payment")'),
|
|
108
|
+
}, async (args) => {
|
|
109
|
+
const query = args.query.toLowerCase();
|
|
110
|
+
const matches = toolIndex.filter(t => t.name.toLowerCase().includes(query) ||
|
|
111
|
+
t.description.toLowerCase().includes(query));
|
|
112
|
+
if (matches.length === 0) {
|
|
113
|
+
return {
|
|
114
|
+
content: [{
|
|
115
|
+
type: 'text',
|
|
116
|
+
text: `No tools found matching "${args.query}". Available tools: ${toolIndex.slice(0, 10).map(t => t.name).join(', ')}${toolIndex.length > 10 ? ` ... and ${toolIndex.length - 10} more` : ''}`,
|
|
117
|
+
}],
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
const resultText = matches.slice(0, 20).map(t => `- **${t.name}**: ${t.description}`).join('\n');
|
|
121
|
+
return {
|
|
122
|
+
content: [{
|
|
123
|
+
type: 'text',
|
|
124
|
+
text: `Found ${matches.length} tools:\n${resultText}\n\nLoad the relevant skill prompt for detailed usage instructions, then call almyty_execute.`,
|
|
125
|
+
}],
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
// =====================================================
|
|
131
|
+
// FULL MODE — registers every tool individually
|
|
132
|
+
// Higher token overhead but works without skill prompts.
|
|
133
|
+
// =====================================================
|
|
134
|
+
for (const tool of tools) {
|
|
135
|
+
const schema = tool.inputSchema || { type: 'object', properties: {} };
|
|
136
|
+
server.tool(tool.name, tool.description || `Tool: ${tool.name}`, schema, async (args) => {
|
|
137
|
+
try {
|
|
138
|
+
const result = await proxy.callTool(tool.name, args);
|
|
139
|
+
return {
|
|
140
|
+
content: [{
|
|
141
|
+
type: 'text',
|
|
142
|
+
text: typeof result === 'string' ? result : JSON.stringify(result, null, 2),
|
|
143
|
+
}],
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
return {
|
|
148
|
+
content: [{ type: 'text', text: `Error: ${error.message}` }],
|
|
149
|
+
isError: true,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// =====================================================
|
|
156
|
+
// SKILLS AS PROMPTS — loaded on demand, not in context
|
|
157
|
+
// =====================================================
|
|
158
|
+
// Each skill is ~100-250 tokens. The LLM loads only the
|
|
159
|
+
// skill it needs via prompts/get, instead of having all
|
|
160
|
+
// N tool schemas (~200 tokens each) always in context.
|
|
161
|
+
// =====================================================
|
|
162
|
+
for (const skill of skills) {
|
|
163
|
+
server.prompt(`skill-${skill.name}`, `How to use: ${skill.name} (${skill.toolCount} tools)`, async () => ({
|
|
164
|
+
messages: [{
|
|
165
|
+
role: 'user',
|
|
166
|
+
content: {
|
|
167
|
+
type: 'text',
|
|
168
|
+
text: skill.content,
|
|
169
|
+
},
|
|
170
|
+
}],
|
|
171
|
+
}));
|
|
172
|
+
}
|
|
173
|
+
// Overview prompt — compact index of all available skills
|
|
174
|
+
if (skills.length > 0 || tools.length > 0) {
|
|
175
|
+
server.prompt('almyty-overview', `Overview: ${tools.length} API tools available via almyty`, async () => {
|
|
176
|
+
const lines = [
|
|
177
|
+
'# almyty API Tools',
|
|
178
|
+
'',
|
|
179
|
+
`Connected to: ${APIFAI_URL}`,
|
|
180
|
+
APIFAI_GATEWAY_ID ? `Gateway: ${APIFAI_GATEWAY_ID}` : '',
|
|
181
|
+
'',
|
|
182
|
+
`## ${tools.length} tools available`,
|
|
183
|
+
'',
|
|
184
|
+
...tools.map(t => `- \`${t.name}\`: ${t.description || 'No description'}`),
|
|
185
|
+
'',
|
|
186
|
+
];
|
|
187
|
+
if (skills.length > 0) {
|
|
188
|
+
lines.push(`## ${skills.length} skills (detailed usage guides)`, '', ...skills.map(s => `- \`skill-${s.name}\`: ${s.toolCount} tools`), '');
|
|
189
|
+
}
|
|
190
|
+
lines.push('## How to use', '', '1. Load a skill prompt to understand the workflow', '2. Call `almyty_execute` with `tool_name` and `parameters`', '3. Or use `almyty_search` to find the right tool first', '');
|
|
191
|
+
return {
|
|
192
|
+
messages: [{
|
|
193
|
+
role: 'user',
|
|
194
|
+
content: { type: 'text', text: lines.filter(Boolean).join('\n') },
|
|
195
|
+
}],
|
|
196
|
+
};
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
// Server info resource
|
|
200
|
+
server.resource('almyty-info', 'almyty://info', async (uri) => ({
|
|
201
|
+
contents: [{
|
|
202
|
+
uri: uri.href,
|
|
203
|
+
mimeType: 'application/json',
|
|
204
|
+
text: JSON.stringify({
|
|
205
|
+
server: APIFAI_URL,
|
|
206
|
+
gatewayId: APIFAI_GATEWAY_ID || 'all',
|
|
207
|
+
mode: APIFAI_MODE,
|
|
208
|
+
tools: tools.length,
|
|
209
|
+
skills: skills.length,
|
|
210
|
+
}, null, 2),
|
|
211
|
+
}],
|
|
212
|
+
}));
|
|
213
|
+
// Connect via stdio
|
|
214
|
+
const transport = new StdioServerTransport();
|
|
215
|
+
await server.connect(transport);
|
|
216
|
+
}
|
|
217
|
+
// Handle subcommands
|
|
218
|
+
const subcommand = process.argv[2];
|
|
219
|
+
if (subcommand === 'login') {
|
|
220
|
+
const { login } = await import('./auth.js');
|
|
221
|
+
await login(APIFAI_URL);
|
|
222
|
+
}
|
|
223
|
+
else if (subcommand === 'logout') {
|
|
224
|
+
const { logout } = await import('./auth.js');
|
|
225
|
+
logout();
|
|
226
|
+
console.log('Logged out successfully.');
|
|
227
|
+
}
|
|
228
|
+
else if (subcommand === '--help' || subcommand === '-h') {
|
|
229
|
+
console.log(`
|
|
230
|
+
@almyty/mcp-server — Skill-first API proxy for any LLM
|
|
231
|
+
|
|
232
|
+
Turn any API into AI skills. Instead of dumping tool schemas into context
|
|
233
|
+
(expensive), this injects compact skills that teach the LLM workflows,
|
|
234
|
+
plus a single universal executor.
|
|
235
|
+
|
|
236
|
+
Token overhead comparison:
|
|
237
|
+
Traditional MCP: 20 tools = ~4,000 tokens/turn (always in context)
|
|
238
|
+
Skill-first: 2 tools = ~300 tokens/turn (skills loaded on demand)
|
|
239
|
+
|
|
240
|
+
Usage:
|
|
241
|
+
npx @almyty/mcp-server Start server (skill-first mode)
|
|
242
|
+
npx @almyty/mcp-server login Interactive login
|
|
243
|
+
npx @almyty/mcp-server logout Clear credentials
|
|
244
|
+
|
|
245
|
+
Environment:
|
|
246
|
+
APIFAI_URL Base URL (default: http://localhost:4000)
|
|
247
|
+
APIFAI_TOKEN JWT Bearer token
|
|
248
|
+
APIFAI_GATEWAY_ID Scope to specific gateway
|
|
249
|
+
APIFAI_MODE "skill-first" (default) | "full" (all tools individually)
|
|
250
|
+
|
|
251
|
+
Modes:
|
|
252
|
+
skill-first 2 tools (almyty_execute + almyty_search) + skills as prompts
|
|
253
|
+
Minimal token overhead. LLM loads skills on demand.
|
|
254
|
+
full All tools registered individually (traditional MCP)
|
|
255
|
+
Higher overhead but works without prompt loading.
|
|
256
|
+
|
|
257
|
+
Configuration:
|
|
258
|
+
|
|
259
|
+
Claude Code: claude mcp add almyty -- npx -y @almyty/mcp-server
|
|
260
|
+
Cursor: .cursor/mcp.json → { "mcpServers": { "almyty": { "command": "npx", "args": ["-y", "@almyty/mcp-server"] } } }
|
|
261
|
+
Codex: ~/.codex/config.toml → [mcp_servers.almyty] command="npx" args=["-y","@almyty/mcp-server"]
|
|
262
|
+
Copilot: .vscode/mcp.json → { "servers": { "almyty": { "command": "npx", "args": ["-y", "@almyty/mcp-server"] } } }
|
|
263
|
+
Gemini: ~/.gemini/settings.json → { "mcpServers": { "almyty": { ... } } }
|
|
264
|
+
`);
|
|
265
|
+
}
|
|
266
|
+
else {
|
|
267
|
+
main().catch((err) => {
|
|
268
|
+
console.error('Fatal error:', err.message);
|
|
269
|
+
process.exit(1);
|
|
270
|
+
});
|
|
271
|
+
}
|
package/dist/proxy.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP proxy to the almyty backend.
|
|
3
|
+
* Fetches tools and executes tool calls via the MCP JSON-RPC API.
|
|
4
|
+
*/
|
|
5
|
+
export interface McpToolDefinition {
|
|
6
|
+
name: string;
|
|
7
|
+
description?: string;
|
|
8
|
+
inputSchema: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
export declare class AlmytyProxy {
|
|
11
|
+
private baseUrl;
|
|
12
|
+
private token;
|
|
13
|
+
private gatewayId?;
|
|
14
|
+
constructor(baseUrl: string, token: string, gatewayId?: string);
|
|
15
|
+
/**
|
|
16
|
+
* Fetch available tools from the almyty backend.
|
|
17
|
+
*/
|
|
18
|
+
fetchTools(): Promise<McpToolDefinition[]>;
|
|
19
|
+
/**
|
|
20
|
+
* Fetch skills from the almyty backend and return as resource content.
|
|
21
|
+
*/
|
|
22
|
+
fetchSkills(): Promise<Array<{
|
|
23
|
+
name: string;
|
|
24
|
+
content: string;
|
|
25
|
+
toolCount: number;
|
|
26
|
+
}>>;
|
|
27
|
+
/**
|
|
28
|
+
* Execute a tool call via the almyty backend.
|
|
29
|
+
*/
|
|
30
|
+
callTool(toolName: string, args: Record<string, unknown>): Promise<unknown>;
|
|
31
|
+
}
|
package/dist/proxy.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP proxy to the almyty backend.
|
|
3
|
+
* Fetches tools and executes tool calls via the MCP JSON-RPC API.
|
|
4
|
+
*/
|
|
5
|
+
export class AlmytyProxy {
|
|
6
|
+
baseUrl;
|
|
7
|
+
token;
|
|
8
|
+
gatewayId;
|
|
9
|
+
constructor(baseUrl, token, gatewayId) {
|
|
10
|
+
this.baseUrl = baseUrl.replace(/\/$/, '');
|
|
11
|
+
this.token = token;
|
|
12
|
+
this.gatewayId = gatewayId;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Fetch available tools from the almyty backend.
|
|
16
|
+
*/
|
|
17
|
+
async fetchTools() {
|
|
18
|
+
const endpoint = this.gatewayId
|
|
19
|
+
? `${this.baseUrl}/api/gateways/${this.gatewayId}/mcp`
|
|
20
|
+
: `${this.baseUrl}/api/mcp`;
|
|
21
|
+
const response = await fetch(endpoint, {
|
|
22
|
+
method: 'POST',
|
|
23
|
+
headers: {
|
|
24
|
+
'Content-Type': 'application/json',
|
|
25
|
+
'Authorization': `Bearer ${this.token}`,
|
|
26
|
+
},
|
|
27
|
+
body: JSON.stringify({
|
|
28
|
+
jsonrpc: '2.0',
|
|
29
|
+
id: 1,
|
|
30
|
+
method: 'tools/list',
|
|
31
|
+
params: {},
|
|
32
|
+
}),
|
|
33
|
+
});
|
|
34
|
+
if (!response.ok) {
|
|
35
|
+
const text = await response.text();
|
|
36
|
+
throw new Error(`Failed to fetch tools (${response.status}): ${text}`);
|
|
37
|
+
}
|
|
38
|
+
const data = await response.json();
|
|
39
|
+
if (data.error) {
|
|
40
|
+
throw new Error(`MCP error: ${data.error.message}`);
|
|
41
|
+
}
|
|
42
|
+
return data.result?.tools || [];
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Fetch skills from the almyty backend and return as resource content.
|
|
46
|
+
*/
|
|
47
|
+
async fetchSkills() {
|
|
48
|
+
const endpoint = this.gatewayId
|
|
49
|
+
? `${this.baseUrl}/api/gateways/${this.gatewayId}/mcp`
|
|
50
|
+
: `${this.baseUrl}/api/mcp`;
|
|
51
|
+
const response = await fetch(endpoint, {
|
|
52
|
+
method: 'POST',
|
|
53
|
+
headers: {
|
|
54
|
+
'Content-Type': 'application/json',
|
|
55
|
+
'Authorization': `Bearer ${this.token}`,
|
|
56
|
+
},
|
|
57
|
+
body: JSON.stringify({
|
|
58
|
+
jsonrpc: '2.0',
|
|
59
|
+
id: 1,
|
|
60
|
+
method: 'skills/list',
|
|
61
|
+
params: { limit: 100 },
|
|
62
|
+
}),
|
|
63
|
+
});
|
|
64
|
+
if (!response.ok) {
|
|
65
|
+
return [];
|
|
66
|
+
}
|
|
67
|
+
const data = await response.json();
|
|
68
|
+
return data.result?.skills || [];
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Execute a tool call via the almyty backend.
|
|
72
|
+
*/
|
|
73
|
+
async callTool(toolName, args) {
|
|
74
|
+
const endpoint = this.gatewayId
|
|
75
|
+
? `${this.baseUrl}/api/gateways/${this.gatewayId}/mcp`
|
|
76
|
+
: `${this.baseUrl}/api/mcp`;
|
|
77
|
+
const response = await fetch(endpoint, {
|
|
78
|
+
method: 'POST',
|
|
79
|
+
headers: {
|
|
80
|
+
'Content-Type': 'application/json',
|
|
81
|
+
'Authorization': `Bearer ${this.token}`,
|
|
82
|
+
},
|
|
83
|
+
body: JSON.stringify({
|
|
84
|
+
jsonrpc: '2.0',
|
|
85
|
+
id: Date.now(),
|
|
86
|
+
method: 'tools/call',
|
|
87
|
+
params: {
|
|
88
|
+
name: toolName,
|
|
89
|
+
arguments: args,
|
|
90
|
+
},
|
|
91
|
+
}),
|
|
92
|
+
});
|
|
93
|
+
if (!response.ok) {
|
|
94
|
+
const text = await response.text();
|
|
95
|
+
throw new Error(`Tool execution failed (${response.status}): ${text}`);
|
|
96
|
+
}
|
|
97
|
+
const data = await response.json();
|
|
98
|
+
if (data.error) {
|
|
99
|
+
throw new Error(`Tool error: ${data.error.message}`);
|
|
100
|
+
}
|
|
101
|
+
// Extract text content from MCP response
|
|
102
|
+
const content = data.result?.content;
|
|
103
|
+
if (Array.isArray(content) && content.length > 0) {
|
|
104
|
+
const textContent = content.find((c) => c.type === 'text');
|
|
105
|
+
if (textContent?.text) {
|
|
106
|
+
try {
|
|
107
|
+
return JSON.parse(textContent.text);
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
return textContent.text;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return data.result;
|
|
115
|
+
}
|
|
116
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@almyty/mcp-server",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Skill-first API proxy for any LLM — turns APIs into AI skills via MCP with minimal token overhead",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"bin": {
|
|
8
|
+
"almyty-mcp": "dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc",
|
|
15
|
+
"dev": "tsx src/index.ts",
|
|
16
|
+
"prepublishOnly": "npm run build"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"mcp",
|
|
20
|
+
"model-context-protocol",
|
|
21
|
+
"ai-tools",
|
|
22
|
+
"api-gateway",
|
|
23
|
+
"openapi",
|
|
24
|
+
"graphql",
|
|
25
|
+
"almyty",
|
|
26
|
+
"claude",
|
|
27
|
+
"cursor",
|
|
28
|
+
"copilot",
|
|
29
|
+
"codex"
|
|
30
|
+
],
|
|
31
|
+
"author": "almyty",
|
|
32
|
+
"license": "BSL-1.1",
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@modelcontextprotocol/sdk": "^1.7.0",
|
|
35
|
+
"zod": "^3.22.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/node": "^25.5.0",
|
|
39
|
+
"tsx": "^4.7.0",
|
|
40
|
+
"typescript": "^5.9.3"
|
|
41
|
+
}
|
|
42
|
+
}
|