I was building an API in APIM which would sit in front of a Logic App so I can plug the API as an MCP server into Claude.

The intention was claude would call my MCP server passing over a name as part of a url path and id then in the policy build a json message and transform my method from GET to POST and send info downsteam. I was getting the below error

{"error":{"code":"InvalidRequestContent","message":"The request content is not valid and could not be deserialized: 'Unexpected character encountered while parsing value: v. Path '', line 0, position 0.'."}}

I was struggling to figure out what was going on so I added App Insights and could see the below.

My problem was that I hadnt formatted my C# code in the policy properly with @{ and } around the code so it was treating my code as the message body rather than returning the value from my JObject reference in C#.

@{
[Your custom code goes here]
}

What my set body should have looked like is below!

<set-body template="none">@{
            var name = ((string)context.Variables["Name"]).Replace("'", "''");
                    
            var body = new JObject(
                new JProperty("name", name)
            );
            return body.ToString();
        }</set-body>

In this case it just treat the code like text and returned it which is a valid thing from the APIM policy perspective.

This was a definitely rusty developer, replace developer issue.

 

Buy Me A Coffee