Initial Commit
This commit is contained in:
@@ -1,2 +1,6 @@
|
|||||||
# TSMCP
|
# TSMCP
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## use inspector to test mcp server
|
||||||
|
https://www.npmjs.com/package/@modelcontextprotocol/inspector
|
||||||
13
package.json
Normal file
13
package.json
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"inspector": "npx @modelcontextprotocol/inspector tsx src/main.ts"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@modelcontextprotocol/sdk": "^1.8.0",
|
||||||
|
"zod": "^3.24.2"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"tsx": "^4.19.3"
|
||||||
|
}
|
||||||
|
}
|
||||||
94
src/main.ts
Normal file
94
src/main.ts
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
// Create an MCP server
|
||||||
|
const server = new McpServer({
|
||||||
|
name: "Demo",
|
||||||
|
version: "1.0.0"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add an addition tool
|
||||||
|
server.tool("add",
|
||||||
|
{ a: z.number(), b: z.number() },
|
||||||
|
async ({ a, b }) => ({
|
||||||
|
content: [{ type: "text", text: String(a + b) }]
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// Adiciona uma ferramenta que se comunica com o Ollama
|
||||||
|
server.tool("ollama",
|
||||||
|
{
|
||||||
|
prompt: z.string(),
|
||||||
|
model: z.string().default("llama3"),
|
||||||
|
options: z.object({
|
||||||
|
temperature: z.number().min(0).max(2).optional().default(0.7),
|
||||||
|
max_tokens: z.number().optional().default(500)
|
||||||
|
}).optional().default({})
|
||||||
|
},
|
||||||
|
async ({ prompt, model, options }) => {
|
||||||
|
try {
|
||||||
|
console.log(`Enviando para Ollama: modelo=${model}, prompt="${prompt.substring(0, 30)}..."`);
|
||||||
|
|
||||||
|
// A API do Ollama espera os parâmetros no corpo da solicitação
|
||||||
|
// e não dentro de um objeto options separado
|
||||||
|
const response = await fetch("http://localhost:11434/api/generate", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
model: model,
|
||||||
|
prompt: prompt,
|
||||||
|
stream: false,
|
||||||
|
temperature: options.temperature,
|
||||||
|
num_predict: options.max_tokens
|
||||||
|
// Remova o objeto options aninhado
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.text();
|
||||||
|
console.error(`Erro na resposta do Ollama: ${error}`);
|
||||||
|
return {
|
||||||
|
content: [{ type: "text", text: `Erro ao chamar Ollama: ${error}` }]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
console.log("Resposta recebida do Ollama");
|
||||||
|
|
||||||
|
if (!data.response) {
|
||||||
|
console.error("Formato de resposta inesperado:", data);
|
||||||
|
return {
|
||||||
|
content: [{ type: "text", text: `Erro: formato de resposta inesperado do Ollama` }]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
content: [{ type: "text", text: data.response }]
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Erro na comunicação com Ollama:`, error);
|
||||||
|
return {
|
||||||
|
content: [{ type: "text", text: `Erro ao conectar com Ollama: ${error.message}` }]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Add a dynamic greeting resource
|
||||||
|
server.resource(
|
||||||
|
"greeting",
|
||||||
|
new ResourceTemplate("greeting://{name}", { list: undefined }),
|
||||||
|
async (uri, { name }) => ({
|
||||||
|
contents: [{
|
||||||
|
uri: uri.href,
|
||||||
|
text: `Hello, ${name}!`
|
||||||
|
}]
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// Start receiving messages on stdin and sending messages on stdout
|
||||||
|
const transport = new StdioServerTransport();
|
||||||
|
await server.connect(transport);
|
||||||
5
tsconfig.json
Normal file
5
tsconfig.json
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"module": "ESNext"
|
||||||
|
},
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user