Add research page with MCP write tools, longer hold for repeat offenders and svn/hg decoys
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a25072e6fb
commit
e88dc415f9
48 files changed
+1983
-57
No files matched your search
@@ -1,3 +1,4 @@
|
||||
using System.Security.Claims;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Flypaper.Storage.Settings;
|
||||
@@ -18,7 +19,9 @@ public static class McpExtensions
|
||||
"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.";
|
||||
"and bytes sent. Most tools only read that data; times are ISO 8601 UTC, durations in seconds. The tools " +
|
||||
"for the Erkenntnisse page (lab notebook entries and ideas) can also write, but only with a token that " +
|
||||
"has write permission; read-only tokens do not see the write tools.";
|
||||
|
||||
private static readonly JsonSerializerOptions ToolJsonOptions = new(McpJsonUtilities.DefaultOptions)
|
||||
{
|
||||
@@ -44,32 +47,64 @@ public static class McpExtensions
|
||||
})
|
||||
.WithHttpTransport(options => options.Stateless = true)
|
||||
.WithTools<FlypaperTools>(ToolJsonOptions)
|
||||
.WithRequestFilters(filters => filters.AddCallToolFilter(next => async (context, ct) =>
|
||||
.WithTools<ResearchTools>(ToolJsonOptions)
|
||||
.WithRequestFilters(filters =>
|
||||
{
|
||||
var tool = context.Params?.Name;
|
||||
var logger = context.Services?.GetRequiredService<ILoggerFactory>().CreateLogger(typeof(FlypaperTools));
|
||||
|
||||
if (context.MatchedPrimitive is McpServerTool matched
|
||||
&& McpArgumentCheck.FindProblem(matched.ProtocolTool.InputSchema, context.Params?.Arguments?.AsReadOnly()) is { } problem)
|
||||
{
|
||||
logger?.LogWarning("MCP tool {Tool} was called with invalid arguments: {Problem}", tool, problem);
|
||||
return InvalidArguments(tool, problem);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return await next(context, ct);
|
||||
}
|
||||
catch (ArgumentException exception)
|
||||
{
|
||||
logger?.LogWarning(exception, "MCP tool {Tool} was called with invalid arguments", tool);
|
||||
return InvalidArguments(tool, exception.Message);
|
||||
}
|
||||
}));
|
||||
filters.AddListToolsFilter(HideWriteTools);
|
||||
filters.AddCallToolFilter(CheckCall);
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private static McpRequestHandler<ListToolsRequestParams, ListToolsResult> HideWriteTools(McpRequestHandler<ListToolsRequestParams, ListToolsResult> next) =>
|
||||
async (context, ct) =>
|
||||
{
|
||||
var result = await next(context, ct);
|
||||
if (!CanWrite(context.User))
|
||||
{
|
||||
result.Tools = result.Tools.Where(tool => tool.Annotations?.ReadOnlyHint == true).ToList();
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
private static McpRequestHandler<CallToolRequestParams, CallToolResult> CheckCall(McpRequestHandler<CallToolRequestParams, CallToolResult> next) =>
|
||||
async (context, ct) =>
|
||||
{
|
||||
var tool = context.Params?.Name;
|
||||
var logger = context.Services?.GetRequiredService<ILoggerFactory>().CreateLogger(typeof(FlypaperTools));
|
||||
|
||||
if (context.MatchedPrimitive is McpServerTool { ProtocolTool.Annotations.ReadOnlyHint: not true } && !CanWrite(context.User))
|
||||
{
|
||||
logger?.LogWarning("MCP tool {Tool} was called with a token without write permission", tool);
|
||||
return new CallToolResult
|
||||
{
|
||||
IsError = true,
|
||||
Content = [new TextContentBlock { Text = $"'{tool}' needs a token with write permission. Enable it for this token in the dashboard under Einstellungen > MCP-Zugriff." }]
|
||||
};
|
||||
}
|
||||
|
||||
if (context.MatchedPrimitive is McpServerTool matched
|
||||
&& McpArgumentCheck.FindProblem(matched.ProtocolTool.InputSchema, context.Params?.Arguments?.AsReadOnly()) is { } problem)
|
||||
{
|
||||
logger?.LogWarning("MCP tool {Tool} was called with invalid arguments: {Problem}", tool, problem);
|
||||
return InvalidArguments(tool, problem);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return await next(context, ct);
|
||||
}
|
||||
catch (ArgumentException exception)
|
||||
{
|
||||
logger?.LogWarning(exception, "MCP tool {Tool} was called with invalid arguments", tool);
|
||||
return InvalidArguments(tool, exception.Message);
|
||||
}
|
||||
};
|
||||
|
||||
private static bool CanWrite(ClaimsPrincipal? user) => user?.HasClaim(McpTokenAuthenticationHandler.WriteClaim, "true") == true;
|
||||
|
||||
private static CallToolResult InvalidArguments(string? tool, string message) => new()
|
||||
{
|
||||
IsError = true,
|
||||
@@ -91,6 +126,18 @@ public static class McpExtensions
|
||||
return Results.LocalRedirect("/settings?mcp=revoked#mcp");
|
||||
});
|
||||
|
||||
app.MapPost("/mcp-tokens/{id:int}/write", async (int id, HttpContext context, IAntiforgery antiforgery, McpTokenStore store) =>
|
||||
{
|
||||
if (!await antiforgery.IsRequestValidAsync(context))
|
||||
{
|
||||
return Results.BadRequest();
|
||||
}
|
||||
|
||||
var enabled = (await context.Request.ReadFormAsync(context.RequestAborted))["enabled"] == "true";
|
||||
await store.SetCanWriteAsync(id, enabled, context.RequestAborted);
|
||||
return Results.LocalRedirect("/settings?mcp=" + (enabled ? "write-on" : "write-off") + "#mcp");
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user