File size: 1,324 Bytes
3d23b0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { FastifyPluginAsync } from 'fastify';
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { streamableHttp, Sessions } from "fastify-mcp";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import { Server } from "@modelcontextprotocol/sdk/server/index.js";

import { registerAllTools } from "./tools";
import { registerAllPrompts } from "./prompts";
import { registerAllResources } from "./resources";

function createMcpServer(): Server {
    const mcp = new McpServer({
        name: "vortex",
        version: "1.0.0",
    });

    registerAllTools(mcp);
    registerAllPrompts(mcp);
    registerAllResources(mcp);

    console.log("[MCP] Server initialized with tools, prompts and resources");

    return mcp.server;
}

export const mcpPlugin: FastifyPluginAsync = async (fastify) => {
    fastify.addHook('onRoute', (routeOptions) => {
        if (routeOptions.url.startsWith('/mcp')) {
            routeOptions.schema = {
                ...routeOptions.schema,
                tags: ['MCP'],
            };
        }
    });

    fastify.register(streamableHttp, {
        stateful: true,
        mcpEndpoint: "/mcp",
        sessions: new Sessions<StreamableHTTPServerTransport>(),
        createServer: createMcpServer,
    });
};