@agentlify/mcp-server 2.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.
@@ -0,0 +1,236 @@
1
+ # Routing Strategies Explained
2
+
3
+ Understanding how Agentlify selects models for your requests.
4
+
5
+ ---
6
+
7
+ ## Overview
8
+
9
+ Agentlify routers use **intelligent routing** to automatically select the best model based on your optimization preferences.
10
+
11
+ ---
12
+
13
+ ## Three Main Strategies
14
+
15
+ ### 1. Cost-Optimized 💰
16
+
17
+ **Best for:** High-volume applications, startups, cost-sensitive workloads
18
+
19
+ **How it works:**
20
+
21
+ - Prioritizes cheapest models that meet quality requirements
22
+ - Routes to budget-friendly models (GPT-4o-mini, Claude Haiku, Gemini Flash)
23
+ - Falls back to premium models only when necessary
24
+
25
+ **Typical savings:** 50-60% vs. using only premium models
26
+
27
+ **Example use cases:**
28
+
29
+ - Customer support chatbots
30
+ - Content moderation
31
+ - Data classification
32
+ - Simple Q&A systems
33
+
34
+ ---
35
+
36
+ ### 2. Quality-Optimized 🎯
37
+
38
+ **Best for:** Content creation, complex reasoning, critical applications
39
+
40
+ **How it works:**
41
+
42
+ - Prioritizes highest-quality models (GPT-4, Claude Opus, Gemini Pro)
43
+ - Considers cost as secondary factor
44
+ - Ensures best possible responses
45
+
46
+ **Typical cost:** 10-20% higher than cost-optimized, but still 30-40% savings vs. single provider
47
+
48
+ **Example use cases:**
49
+
50
+ - Professional content writing
51
+ - Complex analysis
52
+ - Research assistance
53
+ - Creative applications
54
+
55
+ ---
56
+
57
+ ### 3. Speed-Optimized âš¡
58
+
59
+ **Best for:** Real-time applications, user-facing chat, time-sensitive tasks
60
+
61
+ **How it works:**
62
+
63
+ - Prioritizes fastest models (GPT-4o-mini, Claude Haiku, Gemini Flash)
64
+ - Minimizes latency over cost
65
+ - Routes to geographically closer providers
66
+
67
+ **Typical latency:** 200-500ms response times
68
+
69
+ **Example use cases:**
70
+
71
+ - Live chat applications
72
+ - Real-time code completion
73
+ - Interactive assistants
74
+ - Gaming NPCs
75
+
76
+ ---
77
+
78
+ ## How Selection Works
79
+
80
+ ### 1. Request Analysis
81
+
82
+ Router analyzes:
83
+
84
+ - Message complexity
85
+ - Expected response length
86
+ - Required capabilities (vision, tools, etc.)
87
+
88
+ ### 2. Model Filtering
89
+
90
+ Filters models based on:
91
+
92
+ - Capabilities required
93
+ - Budget constraints
94
+ - Performance requirements
95
+
96
+ ### 3. Score Calculation
97
+
98
+ Scores each model based on:
99
+
100
+ ```
101
+ Score = (costWeight × costScore) +
102
+ (qualityWeight × qualityScore) +
103
+ (latencyWeight × latencyScore)
104
+ ```
105
+
106
+ ### 4. Model Selection
107
+
108
+ Selects highest-scoring model and routes request
109
+
110
+ ---
111
+
112
+ ## Configuration
113
+
114
+ ### Dashboard
115
+
116
+ Set weights when creating your router:
117
+
118
+ - Cost Priority: 0-100%
119
+ - Quality Priority: 0-100%
120
+ - Speed Priority: 0-100%
121
+
122
+ ### Via API
123
+
124
+ ```javascript
125
+ const router = await createRouter({
126
+ name: 'my-router',
127
+ routing_strategy: 'cost_optimized', // or "quality_optimized", "speed_optimized"
128
+ });
129
+ ```
130
+
131
+ ---
132
+
133
+ ## Real-World Examples
134
+
135
+ ### Example 1: Cost-Optimized Chatbot
136
+
137
+ **Configuration:**
138
+
139
+ - Cost: 60%
140
+ - Quality: 30%
141
+ - Speed: 10%
142
+
143
+ **Typical routing:**
144
+
145
+ - Simple queries → GPT-4o-mini ($0.15/1M tokens)
146
+ - Complex queries → Claude 3.5 Sonnet ($3/1M tokens)
147
+ - Fallback → GPT-4 ($10/1M tokens)
148
+
149
+ **Monthly cost:** $45 (vs. $100 with GPT-4 only)
150
+
151
+ ---
152
+
153
+ ### Example 2: Quality-Optimized Content
154
+
155
+ **Configuration:**
156
+
157
+ - Quality: 50%
158
+ - Cost: 30%
159
+ - Speed: 20%
160
+
161
+ **Typical routing:**
162
+
163
+ - All requests → GPT-4, Claude 3.5 Sonnet, or Gemini Pro
164
+ - Never uses budget models unless premium models unavailable
165
+
166
+ **Monthly cost:** $70 (vs. $100 with single provider)
167
+
168
+ ---
169
+
170
+ ### Example 3: Speed-Optimized Chat
171
+
172
+ **Configuration:**
173
+
174
+ - Speed: 50%
175
+ - Quality: 30%
176
+ - Cost: 20%
177
+
178
+ **Typical routing:**
179
+
180
+ - Most requests → GPT-4o-mini, Claude Haiku (< 500ms)
181
+ - Complex requests → GPT-4-Turbo (< 1000ms)
182
+ - Fallback → Claude 3.5 Sonnet
183
+
184
+ **Average latency:** 350ms
185
+
186
+ ---
187
+
188
+ ## Best Practices
189
+
190
+ ### Start Cost-Optimized
191
+
192
+ Begin with cost-optimized strategy, then adjust based on:
193
+
194
+ - User feedback on quality
195
+ - Actual latency requirements
196
+ - Budget constraints
197
+
198
+ ### Monitor & Adjust
199
+
200
+ Use dashboard analytics to:
201
+
202
+ - Track model distribution
203
+ - Measure average costs
204
+ - Monitor response quality
205
+ - Adjust weights accordingly
206
+
207
+ ### Use MCP Tools
208
+
209
+ ```
210
+ User: "Optimize my router"
211
+
212
+ AI Assistant:
213
+ Current: Cost 40%, Quality 30%, Speed 30%
214
+ Recommendation: Increase cost to 60%
215
+ Potential savings: $25/month
216
+ ```
217
+
218
+ ---
219
+
220
+ ## Common Questions
221
+
222
+ ### Q: Can I force a specific model?
223
+
224
+ **A:** Routers work best with all models. Forcing specific models defeats the purpose of intelligent routing.
225
+
226
+ ### Q: How often does routing change?
227
+
228
+ **A:** Router selects best model for **each request** based on current conditions.
229
+
230
+ ### Q: What if my preferred model fails?
231
+
232
+ **A:** Router automatically falls back to next-best model. No manual intervention needed.
233
+
234
+ ---
235
+
236
+ For more configuration options, see [Router Configuration Guide](./router-configuration.md)
@@ -0,0 +1,253 @@
1
+ # JavaScript/TypeScript SDK Guide
2
+
3
+ Complete guide for using Agentlify with JavaScript and TypeScript.
4
+
5
+ ---
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install openai
11
+ # Keep using OpenAI SDK!
12
+ ```
13
+
14
+ ---
15
+
16
+ ## Basic Setup
17
+
18
+ ```javascript
19
+ const { OpenAI } = require('openai');
20
+
21
+ const client = new OpenAI({
22
+ apiKey: process.env.AGENTLIFY_API_KEY, // mp_xxx
23
+ baseURL: `https://agentlify.co/api/router/${process.env.AGENTLIFY_ROUTER_ID}`,
24
+ });
25
+ ```
26
+
27
+ ---
28
+
29
+ ## Chat Completions
30
+
31
+ ### Simple Request
32
+
33
+ ```javascript
34
+ const response = await client.chat.completions.create({
35
+ messages: [{ role: 'user', content: 'What is JavaScript?' }],
36
+ });
37
+
38
+ console.log(response.choices[0].message.content);
39
+ ```
40
+
41
+ ### With System Prompt
42
+
43
+ ```javascript
44
+ const response = await client.chat.completions.create({
45
+ messages: [
46
+ { role: 'system', content: 'You are a helpful coding assistant.' },
47
+ { role: 'user', content: 'Explain async/await' },
48
+ ],
49
+ });
50
+ ```
51
+
52
+ ---
53
+
54
+ ## Streaming
55
+
56
+ ```javascript
57
+ const stream = await client.chat.completions.create({
58
+ messages: [{ role: 'user', content: 'Tell me a story' }],
59
+ stream: true,
60
+ });
61
+
62
+ for await (const chunk of stream) {
63
+ process.stdout.write(chunk.choices[0]?.delta?.content || '');
64
+ }
65
+ ```
66
+
67
+ ---
68
+
69
+ ## Function/Tool Calling
70
+
71
+ ```javascript
72
+ const response = await client.chat.completions.create({
73
+ messages: [{ role: 'user', content: 'Get weather in NYC' }],
74
+ tools: [
75
+ {
76
+ type: 'function',
77
+ function: {
78
+ name: 'get_weather',
79
+ description: 'Get current weather',
80
+ parameters: {
81
+ type: 'object',
82
+ properties: {
83
+ location: { type: 'string' },
84
+ },
85
+ required: ['location'],
86
+ },
87
+ },
88
+ },
89
+ ],
90
+ });
91
+
92
+ if (response.choices[0].message.tool_calls) {
93
+ console.log('Tool calls:', response.choices[0].message.tool_calls);
94
+ }
95
+ ```
96
+
97
+ ---
98
+
99
+ ## JSON Mode
100
+
101
+ ```javascript
102
+ const response = await client.chat.completions.create({
103
+ messages: [
104
+ { role: 'system', content: 'You respond in JSON.' },
105
+ { role: 'user', content: 'Generate book info' },
106
+ ],
107
+ response_format: { type: 'json_object' },
108
+ });
109
+
110
+ const data = JSON.parse(response.choices[0].message.content);
111
+ ```
112
+
113
+ ---
114
+
115
+ ## TypeScript Example
116
+
117
+ ```typescript
118
+ import { OpenAI } from 'openai';
119
+
120
+ const client = new OpenAI({
121
+ apiKey: process.env.AGENTLIFY_API_KEY!,
122
+ baseURL: `https://agentlify.co/api/router/${process.env.AGENTLIFY_ROUTER_ID}`,
123
+ });
124
+
125
+ interface BookInfo {
126
+ title: string;
127
+ author: string;
128
+ year: number;
129
+ }
130
+
131
+ async function generateBook(): Promise<BookInfo> {
132
+ const response = await client.chat.completions.create({
133
+ messages: [
134
+ { role: 'system', content: 'Generate book info as JSON' },
135
+ { role: 'user', content: 'A sci-fi novel' },
136
+ ],
137
+ response_format: { type: 'json_object' },
138
+ });
139
+
140
+ return JSON.parse(response.choices[0].message.content);
141
+ }
142
+ ```
143
+
144
+ ---
145
+
146
+ ## React/Next.js Example
147
+
148
+ ```typescript
149
+ 'use client';
150
+
151
+ import { useState } from 'react';
152
+ import { OpenAI } from 'openai';
153
+
154
+ const client = new OpenAI({
155
+ apiKey: process.env.NEXT_PUBLIC_AGENTLIFY_API_KEY!,
156
+ baseURL: `https://agentlify.co/api/router/${process.env.NEXT_PUBLIC_AGENTLIFY_ROUTER_ID}`,
157
+ dangerouslyAllowBrowser: true // For client-side
158
+ });
159
+
160
+ export default function ChatComponent() {
161
+ const [messages, setMessages] = useState([]);
162
+ const [input, setInput] = useState('');
163
+
164
+ const sendMessage = async () => {
165
+ const newMessages = [...messages, { role: 'user', content: input }];
166
+ setMessages(newMessages);
167
+
168
+ const response = await client.chat.completions.create({
169
+ messages: newMessages
170
+ });
171
+
172
+ setMessages([
173
+ ...newMessages,
174
+ { role: 'assistant', content: response.choices[0].message.content }
175
+ ]);
176
+ };
177
+
178
+ return (
179
+ <div>
180
+ {messages.map((msg, i) => (
181
+ <div key={i}>{msg.role}: {msg.content}</div>
182
+ ))}
183
+ <input value={input} onChange={(e) => setInput(e.target.value)} />
184
+ <button onClick={sendMessage}>Send</button>
185
+ </div>
186
+ );
187
+ }
188
+ ```
189
+
190
+ ---
191
+
192
+ ## Environment Variables
193
+
194
+ ```bash
195
+ # .env.local
196
+ AGENTLIFY_API_KEY=mp_your_api_key
197
+ AGENTLIFY_ROUTER_ID=your_router_id
198
+ ```
199
+
200
+ ---
201
+
202
+ ## Error Handling
203
+
204
+ ```javascript
205
+ try {
206
+ const response = await client.chat.completions.create({
207
+ messages: [{ role: 'user', content: 'Hello' }],
208
+ });
209
+ } catch (error) {
210
+ if (error.status === 401) {
211
+ console.error('Invalid API key');
212
+ } else if (error.status === 429) {
213
+ console.error('Rate limit exceeded');
214
+ } else {
215
+ console.error('Error:', error.message);
216
+ }
217
+ }
218
+ ```
219
+
220
+ ---
221
+
222
+ ## Cost Tracking
223
+
224
+ ```javascript
225
+ const response = await client.chat.completions.create({
226
+ messages: [{ role: 'user', content: 'Hello' }],
227
+ });
228
+
229
+ // Access Agentlify metadata
230
+ console.log('Cost:', response._meta?.cost);
231
+ console.log('Model used:', response._meta?.modelUsed);
232
+ console.log('Latency:', response._meta?.latency, 'ms');
233
+ ```
234
+
235
+ ---
236
+
237
+ ## Framework Integration
238
+
239
+ Works with all frameworks:
240
+
241
+ - ✅ Next.js
242
+ - ✅ React
243
+ - ✅ Express
244
+ - ✅ Nest.js
245
+ - ✅ LangChain.js
246
+ - ✅ LlamaIndex
247
+ - ✅ Vercel AI SDK
248
+
249
+ **Same SDK, same code!**
250
+
251
+ ---
252
+
253
+ For more examples, see [Examples folder](./examples/)
@@ -0,0 +1,52 @@
1
+ # Agentlify Python SDK Reference
2
+
3
+ Use the Python SDK to call routers and agents with an OpenAI-compatible interface.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pip install agentlify
9
+ ```
10
+
11
+ ## Initialize
12
+
13
+ ```python
14
+ from agentlify import Agentlify
15
+
16
+ client = Agentlify(api_key="mp_your_api_key")
17
+ ```
18
+
19
+ ## Router Completion
20
+
21
+ ```python
22
+ response = client.routers.create_completion(
23
+ router="your-router-id",
24
+ messages=[{"role": "user", "content": "Hello"}],
25
+ )
26
+ print(response["choices"][0]["message"]["content"])
27
+ ```
28
+
29
+ ## Agent Completion
30
+
31
+ ```python
32
+ response = client.chat.completions.create(
33
+ model="agent:your-agent-id",
34
+ messages=[{"role": "user", "content": "Summarize this"}],
35
+ )
36
+ print(response["choices"][0]["message"]["content"])
37
+ ```
38
+
39
+ ## Manual Tool Call Continuation
40
+
41
+ If an agent response includes `tool_calls`, execute them in your app and send back:
42
+
43
+ - the assistant message containing `tool_calls`
44
+ - one `role: "tool"` message per tool result with matching `tool_call_id`
45
+
46
+ Then call the same `agent:` model again to continue execution.
47
+
48
+ ## Notes
49
+
50
+ - Keep API keys in environment variables.
51
+ - Pass only schema-valid JSON arguments for tools.
52
+ - For production retries, use idempotency keys when your webhook tools support them.
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@agentlify/mcp-server",
3
+ "version": "2.0.0",
4
+ "description": "Model Context Protocol server for Agentlify - enables AI assistants to integrate Agentlify",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "bin": {
8
+ "agentlify-mcp": "dist/index.js"
9
+ },
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "dev": "tsx watch src/index.ts",
13
+ "start": "node dist/index.js",
14
+ "prepublishOnly": "npm run build"
15
+ },
16
+ "keywords": [
17
+ "mcp",
18
+ "model-context-protocol",
19
+ "agentlify",
20
+ "ai",
21
+ "llm",
22
+ "openai",
23
+ "anthropic",
24
+ "claude",
25
+ "gpt"
26
+ ],
27
+ "author": "Agentlify",
28
+ "license": "MIT",
29
+ "dependencies": {
30
+ "@modelcontextprotocol/sdk": "^1.26.0"
31
+ },
32
+ "devDependencies": {
33
+ "@types/node": "^20.0.0",
34
+ "tsx": "^4.7.0",
35
+ "typescript": "^5.3.0"
36
+ },
37
+ "engines": {
38
+ "node": ">=18.0.0"
39
+ }
40
+ }