Skip to content

Commit 659a2d5

Browse files
committed
feat: add standard /mcp endpoint with SSE support and legacy compatibility
1 parent 86c071b commit 659a2d5

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

.changeset/modern-ideas-tease.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"ansible-database-mcp": minor
3+
---
4+
5+
add standard /mcp endpoint with SSE support and legacy compatibility

src/main.ts

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,36 @@ app.use(cors({
4242

4343

4444

45-
// Apply auth middleware to MCP endpoint
46-
app.post('/', authMiddleware, mcpMiddleware);
45+
// MCP standard endpoint at /mcp
46+
app.post('/mcp', authMiddleware, mcpMiddleware);
47+
48+
// Legacy endpoint at / for backward compatibility - redirect to /mcp
49+
app.post('/', (_req: Request, res: Response) => {
50+
console.log('Redirecting POST / to /mcp');
51+
res.redirect(307, '/mcp');
52+
});
4753

4854
// Health check endpoint
4955
app.get('/health', (_req, res) => {
5056
res.json({
5157
status: 'ok',
5258
name: 'ansible-database',
5359
version: '1.0.0',
54-
auth: getAuthStrategyName()
60+
auth: getAuthStrategyName(),
61+
endpoints: {
62+
mcp: '/mcp',
63+
legacy: '/'
64+
}
5565
});
5666
});
5767

5868

69+
// HEAD request handling for MCP endpoint
5970
if (authStrategy.getName() === 'none') {
71+
app.head('/mcp', (_req, res) => {
72+
res.sendStatus(200);
73+
});
74+
// Legacy support
6075
app.head('/', (_req, res) => {
6176
res.sendStatus(200);
6277
});
@@ -66,6 +81,13 @@ if (isOAuthStrategy(authStrategy)) {
6681
const oauthIssuer = process.env.OAUTH_ISSUER!;
6782
const publicUrl = process.env.PUBLIC_URL || `http://localhost:${PORT}`;
6883

84+
app.head('/mcp', (_req, res) => {
85+
res.status(401)
86+
.set('WWW-Authenticate', `Bearer realm="Ansible Database MCP", error="invalid_token", resource_metadata="${publicUrl}/.well-known/oauth-protected-resource"`)
87+
.end();
88+
});
89+
90+
// Legacy support
6991
app.head('/', (_req, res) => {
7092
res.status(401)
7193
.set('WWW-Authenticate', `Bearer realm="Ansible Database MCP", error="invalid_token", resource_metadata="${publicUrl}/.well-known/oauth-protected-resource"`)
@@ -84,28 +106,39 @@ if (isOAuthStrategy(authStrategy)) {
84106
});
85107
}
86108

109+
// Handle GET requests to MCP endpoint - delegate to SDK for SSE support
110+
app.get('/mcp', authMiddleware, mcpMiddleware);
111+
112+
// Legacy GET endpoint - redirect to /mcp
87113
app.get('/', async (_req: Request, res: Response) => {
88-
console.log('Received GET MCP request');
89-
res.writeHead(405).end(JSON.stringify({
114+
console.log('Redirecting GET / to /mcp');
115+
res.redirect(307, '/mcp');
116+
});
117+
118+
// Handle DELETE requests to MCP endpoint
119+
app.delete('/mcp', async (_req: Request, res: Response) => {
120+
console.log('Received DELETE /mcp request');
121+
res.status(405).json({
90122
jsonrpc: "2.0",
91123
error: {
92124
code: -32000,
93125
message: "Method not allowed."
94126
},
95127
id: null
96-
}));
128+
});
97129
});
98130

131+
// Legacy DELETE endpoint
99132
app.delete('/', async (_req: Request, res: Response) => {
100-
console.log('Received DELETE MCP request');
101-
res.writeHead(405).end(JSON.stringify({
133+
console.log('Received DELETE / request');
134+
res.status(405).json({
102135
jsonrpc: "2.0",
103136
error: {
104137
code: -32000,
105138
message: "Method not allowed."
106139
},
107140
id: null
108-
}));
141+
});
109142
});
110143

111144
// Start server
@@ -114,7 +147,8 @@ const server = app.listen(PORT, () => {
114147
console.log(`\n🚀 MCP server started!`);
115148
console.log(`📍 Port: ${PORT}`);
116149
console.log(`🔐 Authentication: ${getAuthStrategyName()}`);
117-
console.log(`🔗 MCP endpoint: ${publicUrl}`);
150+
console.log(`🔗 MCP endpoint: ${publicUrl}/mcp`);
151+
console.log(`🔗 Legacy endpoint: ${publicUrl}/ (redirects to /mcp)`);
118152
console.log(`💚 Health check: ${publicUrl}/health`);
119153
});
120154

0 commit comments

Comments
 (0)