-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
199 lines (192 loc) · 6.66 KB
/
main.ts
File metadata and controls
199 lines (192 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env -S deno run --allow-all --unstable-kv
/**
* @module examples/1-simple
*
* # Simple MCP Server - Minimal Setup with Basic Plugin Tools
*
* This demonstrates the simplest approach for bb-mcp-server:
* - Zero custom dependencies (uses library defaults)
* - Plugin discovery with basic utility tools
* - Minimal configuration and setup
* - Perfect starting point for learning MCP server development
*
* LEARNING FOCUS: "How to get started with minimal setup"
*
* FEATURES DEMONSTRATED:
* ========================
*
* ✅ Plugin Discovery System:
* - Automatic plugin loading from src/plugins/
* - Self-contained plugin with utility tools
* - No manual registration required
*
* ✅ Basic Tool Development:
* - current_datetime: Basic data retrieval patterns
* - get_system_info: System integration examples
* - validate_json: Data validation and formatting
*
* ✅ Environment Configuration:
* - Simple .env file setup
* - Transport detection (STDIO vs HTTP)
* - Minimal required configuration
*
* ✅ AppServer Defaults:
* - Library handles all infrastructure automatically
* - Logger, TransportManager, WorkflowRegistry created automatically
* - Zero boilerplate dependency injection code
*
* ARCHITECTURE:
* =============
*
* AppServer.create(minimal config)
* ├── Plugin Discovery System (automatic)
* ├── SimplePlugin
* │ ├── current_datetime tool
* │ ├── get_system_info tool
* │ └── validate_json tool
* └── Default Dependencies
* ├── Logger (library default)
* ├── TransportManager (library default)
* ├── WorkflowRegistry (library default)
* └── ConfigManager (library default)
*
* USAGE:
* ======
*
* # STDIO transport (default):
* deno run --allow-all --unstable-kv main.ts
*
* # HTTP transport:
* MCP_TRANSPORT=http deno run --allow-all --unstable-kv main.ts
* # Then access: http://localhost:3000
*
* NEXT STEPS:
* ===========
*
* After mastering this simple example:
* 1. Try 2-plugin-workflows to learn about multi-step processes
* 2. Explore 3-plugin-api-auth for external API integration
* 3. Study 4-manual-deps for complete infrastructure control
*
* @example Run this example directly from JSR
* ```bash
* # Run with STDIO transport (default)
* deno run --allow-all --unstable-kv jsr:@beyondbetter/bb-mcp-server/examples/1-simple
*
* # Run with HTTP transport
* MCP_TRANSPORT=http deno run --allow-all --unstable-kv jsr:@beyondbetter/bb-mcp-server/examples/1-simple
* ```
*
* @example Basic server setup
* ```typescript
* import { AppServer } from 'jsr:@beyondbetter/bb-mcp-server';
*
* const appServer = await AppServer.create({
* serverConfig: {
* name: 'my-mcp-server',
* version: '1.0.0',
* title: 'My MCP Server',
* description: 'My custom MCP server',
* },
* });
*
* await appServer.start();
* ```
*
* @see {@link https://github.com/beyond-better/bb-mcp-server/tree/main/examples/1-simple | Full example documentation}
* @see {@link https://github.com/beyond-better/bb-mcp-server/tree/main/examples | All examples}
*/
// Import the bb-mcp-server library - handles ALL infrastructure
import { AppServer } from '@beyondbetter/bb-mcp-server';
/**
* Simple main function - library handles all complexity
*
* This is the minimal setup pattern:
* 1. Create AppServer with basic configuration
* 2. Start the server
* 3. Library handles everything else automatically
*/
async function main(): Promise<void> {
try {
// 📝 Create AppServer with minimal configuration
// Library automatically:
// - Loads environment variables
// - Creates default dependencies (logger, transport, etc.)
// - Discovers plugins in src/plugins/
// - Registers all discovered tools
// - Configures transport (STDIO or HTTP based on MCP_TRANSPORT)
const appServer = await AppServer.create({
// 🎯 Only required: basic server identification
serverConfig: {
name: 'simple-mcp-server',
version: '1.0.0',
title: 'Simple MCP Server Example',
description:
'Demonstrates basic plugin tools with minimal setup using bb-mcp-server library',
},
});
// 🚀 Start the complete application stack
// This single call handles:
// - MCP server initialization
// - HTTP server startup (if transport=http)
// - Plugin loading and tool registration
// - Complete application lifecycle management
await appServer.start();
console.log('🎉 Simple MCP Server started successfully!');
console.log(
'📝 Available tools: current_datetime, get_system_info, validate_json',
);
console.log('🔄 Transport:', process.env.MCP_TRANSPORT || 'stdio');
} catch (error) {
console.error('❌ Failed to start Simple MCP Server:', error);
Deno.exit(1);
}
}
// 🎯 Clean, simple entry point - library handles ALL complexity
if (import.meta.main) {
main();
}
/**
* ✨ SUCCESS METRICS - Simple Example Implementation:
*
* 📊 CODE SIMPLICITY:
* - Total setup: ~15 lines of meaningful code
* - Zero dependency injection boilerplate
* - Zero infrastructure configuration
* - Maximum focus on business logic (the tools)
*
* 🎯 LEARNING BENEFITS:
* ✅ Zero Infrastructure Code: All moved to AppServer library class
* ✅ Plugin Discovery: Automatic loading and registration
* ✅ Environment Driven: Complete configuration via .env variables
* ✅ Transport Agnostic: Automatic HTTP vs STDIO detection
* ✅ Production Ready: Complete error handling and monitoring
* ✅ Easy to Debug: Clear logging and error messages
*
* 🗺️ LIBRARY HANDLES AUTOMATICALLY:
* - Configuration loading from environment variables
* - Plugin discovery in src/plugins/ directory
* - Tool registration and MCP protocol compliance
* - Transport management (STDIO vs HTTP)
* - Signal handling and graceful shutdown
* - Error handling and application monitoring
* - Complete infrastructure orchestration
*
* 🎨 USER RESPONSIBILITIES (Minimal):
* - Create tools in plugin (business logic only)
* - Configure environment variables as needed
* - Run server with appropriate permissions
* - Focus on solving user problems, not infrastructure
*
* 🛣️ PROGRESSION PATH:
*
* From this simple example, users can:
* 1. Add more tools to SimplePlugin
* 2. Create additional plugins for organization
* 3. Move to 2-plugin-workflows to learn about multi-step processes
* 4. Progress to 3-plugin-api-auth for external integrations
* 5. Master 4-manual-deps for complete infrastructure control
*
* The beauty of this approach: users learn MCP concepts without
* being overwhelmed by infrastructure complexity!
*/