Skip to content

Commit fc035eb

Browse files
author
vcart
committed
feat: implementing FastMCP standard for templates
1 parent aa645bc commit fc035eb

8 files changed

Lines changed: 86 additions & 259 deletions

File tree

bin/create-mcp-server.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import fs from 'fs';
44
import path from 'path';
55
import { fileURLToPath } from 'url';
6-
import { execSync } from 'child_process';
76

87
// Get the directory where the source files are stored
98
const __filename = fileURLToPath(import.meta.url);
@@ -171,7 +170,7 @@ function createProjectPackageJson() {
171170
"typescript": "^5.8.2"
172171
},
173172
dependencies: {
174-
"@modelcontextprotocol/sdk": "^1.7.0",
173+
"fastmcp": "^1.21.0",
175174
"cors": "^2.8.5",
176175
"express": "^4.21.2",
177176
"zod": "^3.24.2"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@
6969
"typescript": "^5.8.2"
7070
},
7171
"dependencies": {
72-
"@modelcontextprotocol/sdk": "^1.7.0",
7372
"cors": "^2.8.5",
7473
"express": "^4.21.2",
74+
"fastmcp": "^1.21.0",
7575
"zod": "^3.24.2"
7676
},
7777
"engines": {

src/core/prompts.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
1+
import { FastMCP } from "fastmcp";
22
import { z } from "zod";
33

44
/**
55
* Register all prompts with the MCP server
6-
* @param server The MCP server instance
6+
* @param server The FastMCP server instance
77
*/
8-
export function registerPrompts(server: McpServer) {
8+
export function registerPrompts(server: FastMCP) {
99
// Example prompt
10-
server.prompt(
11-
"greeting",
12-
"A simple greeting prompt",
13-
{
14-
name: z.string().describe("Name to greet")
15-
},
16-
(params: { name: string }) => ({
17-
messages: [{
18-
role: "user",
19-
content: {
20-
type: "text",
21-
text: `Hello, ${params.name}! How can I help you today?`
22-
}
23-
}]
24-
})
25-
);
10+
server.addPrompt({
11+
name: "greeting",
12+
description: "A simple greeting prompt",
13+
arguments: [
14+
{
15+
name: "name",
16+
description: "Name to greet",
17+
required: true,
18+
},
19+
],
20+
load: async ({ name }) => {
21+
return `Hello, ${name}! How can I help you today?`;
22+
}
23+
});
2624
}

src/core/resources.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
1-
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
1+
import { FastMCP } from "fastmcp";
22
import * as services from "./services/index.js";
33

44
/**
55
* Register all resources with the MCP server
6-
* @param server The MCP server instance
6+
* @param server The FastMCP server instance
77
*/
8-
export function registerResources(server: McpServer) {
8+
export function registerResources(server: FastMCP) {
99
// Example resource
10-
server.resource(
11-
"example_resource",
12-
"example://{id}",
13-
async (uri: URL) => {
14-
const id = uri.pathname.split('/').pop();
10+
server.addResourceTemplate({
11+
uriTemplate: "example://{id}",
12+
name: "Example Resource",
13+
mimeType: "text/plain",
14+
arguments: [
15+
{
16+
name: "id",
17+
description: "Resource ID",
18+
required: true,
19+
},
20+
],
21+
async load({ id }) {
1522
return {
16-
contents: [{
17-
uri: uri.toString(),
18-
text: `This is an example resource with ID: ${id}`
19-
}]
23+
text: `This is an example resource with ID: ${id}`
2024
};
2125
}
22-
);
26+
});
2327
}

src/core/tools.ts

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,36 @@
1-
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
1+
import { FastMCP } from "fastmcp";
22
import { z } from "zod";
33
import * as services from "./services/index.js";
44

55
/**
66
* Register all tools with the MCP server
77
*
8-
* @param server The MCP server instance
8+
* @param server The FastMCP server instance
99
*/
10-
export function registerTools(server: McpServer) {
10+
export function registerTools(server: FastMCP) {
1111
// Greeting tool
12-
server.tool(
13-
"hello_world",
14-
"A simple hello world tool",
15-
{
12+
server.addTool({
13+
name: "hello_world",
14+
description: "A simple hello world tool",
15+
parameters: z.object({
1616
name: z.string().describe("Name to greet")
17-
},
18-
async (params: { name: string }) => {
17+
}),
18+
execute: async (params) => {
1919
const greeting = services.GreetingService.generateGreeting(params.name);
20-
return {
21-
content: [
22-
{
23-
type: "text",
24-
text: greeting
25-
}
26-
]
27-
};
20+
return greeting;
2821
}
29-
);
22+
});
3023

3124
// Farewell tool
32-
server.tool(
33-
"goodbye",
34-
"A simple goodbye tool",
35-
{
25+
server.addTool({
26+
name: "goodbye",
27+
description: "A simple goodbye tool",
28+
parameters: z.object({
3629
name: z.string().describe("Name to bid farewell to")
37-
},
38-
async (params: { name: string }) => {
30+
}),
31+
execute: async (params) => {
3932
const farewell = services.GreetingService.generateFarewell(params.name);
40-
return {
41-
content: [
42-
{
43-
type: "text",
44-
text: farewell
45-
}
46-
]
47-
};
33+
return farewell;
4834
}
49-
);
35+
});
5036
}

src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
1+
import { FastMCP } from "fastmcp";
22
import startServer from "./server/server.js";
33

44
// Start the server
55
async function main() {
66
try {
77
const server = await startServer();
8-
const transport = new StdioServerTransport();
9-
await server.connect(transport);
8+
9+
server.start({
10+
transportType: "stdio",
11+
});
12+
1013
console.error("MCP Server running on stdio");
1114
} catch (error) {
1215
console.error("Error starting MCP server:", error);

0 commit comments

Comments
 (0)