Add read-only MCP server with token access
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b8328f6998
commit
49a8fa1154
34 files changed
+1625
No files matched your search
@@ -0,0 +1,59 @@
|
||||
using Flypaper.Storage.Settings;
|
||||
using Microsoft.AspNetCore.Antiforgery;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using ModelContextProtocol.Protocol;
|
||||
|
||||
namespace Flypaper.Web.Mcp;
|
||||
|
||||
public static class McpExtensions
|
||||
{
|
||||
public const string Path = "/mcp";
|
||||
private const string PolicyName = "Mcp";
|
||||
|
||||
private const string Instructions =
|
||||
"Flypaper is an HTTP tarpit. Scanning bots that probe a server (e.g. for /.env or /wp-login.php) are " +
|
||||
"answered with an endless page a few bytes at a time and held as long as possible. Every such catch is " +
|
||||
"stored with its time, client IP, country, network provider (ASN), requested URL, user agent, duration " +
|
||||
"and bytes sent. These tools only read that data; times are ISO 8601 UTC, durations in seconds.";
|
||||
|
||||
public static IServiceCollection AddFlypaperMcp(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.Configure<McpOptions>(configuration.GetSection(McpOptions.SectionName));
|
||||
services.AddSingleton<McpTokenService>();
|
||||
|
||||
services.AddAuthentication()
|
||||
.AddScheme<AuthenticationSchemeOptions, McpTokenAuthenticationHandler>(McpTokenAuthenticationHandler.SchemeName, null);
|
||||
services.AddAuthorizationBuilder()
|
||||
.AddPolicy(PolicyName, policy => policy
|
||||
.AddAuthenticationSchemes(McpTokenAuthenticationHandler.SchemeName)
|
||||
.RequireAuthenticatedUser());
|
||||
|
||||
services.AddMcpServer(options =>
|
||||
{
|
||||
options.ServerInfo = new Implementation { Name = "flypaper", Version = AppVersion.Current };
|
||||
options.ServerInstructions = Instructions;
|
||||
})
|
||||
.WithHttpTransport(options => options.Stateless = true)
|
||||
.WithTools<FlypaperTools>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IEndpointRouteBuilder MapFlypaperMcp(this IEndpointRouteBuilder app)
|
||||
{
|
||||
app.MapMcp(Path).RequireAuthorization(PolicyName);
|
||||
|
||||
app.MapPost("/mcp-tokens/{id:int}/revoke", async (int id, HttpContext context, IAntiforgery antiforgery, McpTokenStore store) =>
|
||||
{
|
||||
if (!await antiforgery.IsRequestValidAsync(context))
|
||||
{
|
||||
return Results.BadRequest();
|
||||
}
|
||||
|
||||
await store.RevokeAsync(id, context.RequestAborted);
|
||||
return Results.LocalRedirect("/settings?mcp=revoked");
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user