-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathserver.js
More file actions
107 lines (93 loc) · 2.92 KB
/
server.js
File metadata and controls
107 lines (93 loc) · 2.92 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
#!/usr/bin/env node
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import fetch from 'node-fetch';
const OLLAMA_BASE_URL = 'http://localhost:11434';
const DEFAULT_MODEL = 'llama3.2:3b';
async function sendToOllama(prompt, model = DEFAULT_MODEL) {
try {
const response = await fetch(`${OLLAMA_BASE_URL}/api/generate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ model, prompt, stream: false })
});
const result = await response.json();
return result.response;
} catch (error) {
return `Error: ${error.message}`;
}
}
async function main() {
const server = new McpServer({
name: 'fixed-ollama-server',
version: '1.0.0',
});
// Register prompts with handlers but no schemas
server.registerPrompt(
'greeting',
{
title: 'Personalized Greeting',
description: 'Creates a personalized greeting'
},
async () => {
const prompt = "Hello, Alice! Welcome to our MCP prompt demo.";
const response = await sendToOllama(prompt);
return {
messages: [
{ role: 'user', content: { type: 'text', text: prompt } },
{ role: 'assistant', content: { type: 'text', text: response } }
]
};
}
);
server.registerPrompt(
'explain-concept',
{
title: 'Concept Explainer',
description: 'Explains complex concepts simply'
},
async () => {
const prompt = "Please explain quantum computing in simple terms that a beginner could understand. Use examples and analogies where appropriate.";
const response = await sendToOllama(prompt);
return {
messages: [
{ role: 'user', content: { type: 'text', text: prompt } },
{ role: 'assistant', content: { type: 'text', text: response } }
]
};
}
);
server.registerPrompt(
'code-review',
{
title: 'Code Review Assistant',
description: 'Reviews code and provides suggestions'
},
async () => {
const prompt = `Please review the following javascript code and provide suggestions for improvement:
\`\`\`javascript
function hello() { console.log("hi") }
\`\`\`
Please focus on:
- Code quality and best practices
- Performance optimizations
- Security considerations
- Readability and maintainability`;
const response = await sendToOllama(prompt);
return {
messages: [
{ role: 'user', content: { type: 'text', text: prompt } },
{ role: 'assistant', content: { type: 'text', text: response } }
]
};
}
);
console.error('✅ All prompts registered');
const transport = new StdioServerTransport();
await server.connect(transport);
console.error('✅ Server ready');
}
main().catch((error) => {
console.error('❌ Server error:', error);
process.exit(1);
});