Exposing Azure Funtion as MCP Server in Azure API Manager is not passing POST body to API
When I expose my Azure Function as an MCP server through API Management, I find that the expected body doesn’t reach my Azure Function. I get the following error:
“An error has occurred with Inputs args: Expecting value: line 1 column 1 (char 0)”
@app.route(route="search_documents", methods=["POST"])
def search_documents(req: func.HttpRequest) -> func.HttpResponse:
"""
List metadata details of documents stored in a specific SharePoint folder.
Args:
context: The trigger context containing the input arguments.
Returns:
A dictionary containing the metadata for each document, exported in JSON format.
"""
try:
# input args
inputs = loads(req.get_body())
site_url = inputs["site_url"]
folder_path = inputs["folder_path"]
user_email = inputs["user_email"]
document_library = inputs["document_library"]
logger.info("Info: Input arguments have been loaded into the Metadata tool function.")
except Exception as e:
logger.error(f"Metadata tool An error has occurred with Inputs args: {e}")
return func.HttpResponse(dumps({"error":"Metadata tool: An error has occurred with the input arguments. Please validate that they are not empty and are the correct data type."}), status_code=400, mimetype="application/json")
If I add the following policy to each endpoint in API Management, the MCP starts working correctly. However, I notice that performance worsens — for queries that return a lot of information, using the Azure Function directly works fine without reaching a timeout, but when using it through MCP, the requests get stuck:
<policies>
<inbound>
<base />
<!-- Guardar el body original para depuración -->
<set-variable name="originalBody" value="@{
return context.Request.Body.As<string>(preserveContent: true);
}" />
<!-- Enviar el mismo body original al backend -->
<set-body>@(context.Variables.GetValueOrDefault<string>("originalBody"))</set-body>
<set-backend-service id="apim-generated-policy" backend-id="myaftest" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
I’m experiencing issues similar to those in this post: Exposing API as MCP Server in Azure API Manager is not passing POST body to API, but I don’t see that anyone has provided an answer.