okami.mom/web-extract-go
git clone https://git.nkp.pet/okami.mom/web-extract-go
5a90548
main
1package app 2 3import ( 4"encoding/json" 5"fmt" 6"strings" 7"testing" 8) 9 10func TestMCPTransportMetaIsNotForwardedAsToolInput (t * testing.T ) { 11for _ ,tc := range []struct { 12tool ,args string 13 }{ 14 {"web_search" ,`{"query":"request limits","numResults":1}` }, 15 {"web_contents" ,`{"urls":[%q]}` }, 16 {"web_answer" ,`{"query":"request limits"}` }, 17 {"web_extract" ,`{"url":%q}` }, 18 {"web_preview" ,`{"url":%q,"query":"request limits"}` }, 19 {"web_summarize" ,`{"url":%q,"query":"request limits"}` }, 20 } { 21t .Run (tc .tool ,func (t * testing.T ) { 22s ,page := fixtureService (t ) 23args := tc .args 24if strings .Contains (args ,"%q" ) { 25args = fmt .Sprintf (args ,page .URL ) 26 } 27args = strings .TrimSuffix (args ,"}" )+ `,"_meta":{"prime":"not a tool argument"}}` 28body := fmt .Sprintf (`{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":%q,"arguments":%s,"_meta":{"progressToken":17}}}` ,tc .tool ,args ) 29status ,payload := callHTTP (t ,s ,"/mcp" ,body ) 30var result struct { 31IsError bool `json:"isError"` 32StructuredContent json.RawMessage `json:"structuredContent"` 33 } 34if status != 200 || json .Unmarshal (payload ["result" ],& result )!= nil || result .IsError { 35t .Fatalf ("metadata reached tool input: status=%d result=%s" ,status ,payload ["result" ]) 36 } 37if strings .Contains (string (result .StructuredContent ),"prime" ) { 38t .Fatal ("transport metadata leaked into the result" ) 39 } 40var detail string 41if err := s .log .db .QueryRow (`select detail from requests where endpoint in ('search','contents','answer') order by id desc limit 1` ).Scan (& detail );err != nil || strings .Contains (detail ,"prime" ) { 42t .Fatalf ("transport metadata leaked into request log: %s, %v" ,detail ,err ) 43 } 44 }) 45 } 46} 47 48func TestMCPMetaStrippingPreservesStrictArguments (t * testing.T ) { 49s ,page := fixtureService (t ) 50for _ ,tc := range []struct {tool ,args string }{ 51 {"web_extract" ,`{"url":%q,"_meta":{},"unexpected":true}` }, 52 {"web_contents" ,`{"urls":[%q],"_meta":{},"text":{"maxChars":40}}` }, 53 } { 54body := fmt .Sprintf (`{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":%q,"arguments":%s,"_meta":{}}}` ,tc .tool ,fmt .Sprintf (tc .args ,page .URL )) 55_ ,payload := callHTTP (t ,s ,"/mcp" ,body ) 56if ! strings .Contains (string (payload ["result" ]),`"isError":true` ) { 57t .Fatalf ("unknown tool argument was accepted: %s" ,payload ["result" ]) 58 } 59 } 60status ,_ := callHTTP (t ,s ,"/v1/extract" ,fmt .Sprintf (`{"url":%q,"_meta":{}}` ,page .URL )) 61if status != 400 { 62t .Fatalf ("http request unexpectedly accepted mcp metadata: %d" ,status ) 63 } 64}