export const startMcp = async (app: INestApplication) => {
const mastraService = app.get(MastraService, { strict: false });
const organizationService = app.get(OrganizationService, { strict: false });
const mastra = await mastraService.mastra();
const agent = mastra.getAgent('postiz');
const tools = await agent.getTools();
const server = new MCPServer({
name: 'Postiz MCP',
version: '1.0.0',
tools,
agents: { postiz: agent },
});
app.use(
'/mcp/:id',
async (req: Request, res: Response, next: NextFunction) => {
if (req.method === 'OPTIONS') {
res.sendStatus(200);
return;
}
// @ts-ignore
req.auth = await organizationService.getOrgByApiKey(req.params.id);
// @ts-ignore
if (!req.auth) {
throw new HttpException('Invalid url', 400);
}
const url = new URL(
`/mcp/${req.params.id}`,
process.env.NEXT_PUBLIC_BACKEND_URL
);
await server.startHTTP({
url,
httpPath: url.pathname,
options: {
sessionIdGenerator: () => {
return randomUUID();
},
},
req,
res,
});
}
);
};
export const startMcp = async (app: INestApplication) => {
const mastraService = app.get(MastraService, { strict: false });
const organizationService = app.get(OrganizationService, { strict: false });
const mastra = await mastraService.mastra();
const agent = mastra.getAgent('postiz');
const tools = await agent.getTools();
const server = new MCPServer({
name: 'Postiz MCP',
version: '1.0.0',
tools,
agents: { postiz: agent },
});
app.use(
'/mcp/:id',
async (req: Request, res: Response, next: NextFunction) => {
if (req.method === 'OPTIONS') {
res.sendStatus(200);
return;
}
// @ts-ignore
req.auth = await organizationService.getOrgByApiKey(req.params.id);
// @ts-ignore
if (!req.auth) {
throw new HttpException('Invalid url', 400);
}
const url = new URL(
`/mcp/${req.params.id}`,
process.env.NEXT_PUBLIC_BACKEND_URL
);
await server.startHTTP({
url,
httpPath: url.pathname,
options: {
sessionIdGenerator: () => {
return randomUUID();
},
},
req,
res,
});
}
);
};