Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/codeql/reusables/supported-frameworks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ and the CodeQL library pack ``codeql/python-all`` (`changelog <https://github.co
Tornado, Web framework
Twisted, Web framework
Gradio, Web framework
fastmcp, MCP server framework
mcp, MCP server framework
starlette, Asynchronous Server Gateway Interface (ASGI)
ldap3, Lightweight Directory Access Protocol (LDAP)
python-ldap, Lightweight Directory Access Protocol (LDAP)
Expand Down
4 changes: 4 additions & 0 deletions python/ql/lib/change-notes/2026-10-04-mcp-remote-sources.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* Added `ai` threat-model sources for MCP (`mcp` and `fastmcp`) tool, resource, and prompt handlers that are wrapped by inner decorators.
26 changes: 25 additions & 1 deletion python/ql/lib/semmle/python/frameworks/Mcp.qll
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ private import semmle.python.ApiGraphs
* The parameters of tool, resource and prompt handlers are chosen by the MCP client, that is by a
* language model or by anyone who can reach the server. The low-level server of `mcp`, the HTTP
* headers and the bearer tokens are modeled as data extensions (`mcp.model.yml`, `fastmcp.model.yml`).
*
* QL decorator matching on `getADecorator()` covers handlers whose MCP registration is hidden
* behind inner custom project wrappers that the API-graph baseline does not reach.
*/
module Mcp {
/** Gets a reference to a server class whose instances register tool, resource and prompt handlers. */
Expand Down Expand Up @@ -122,6 +125,23 @@ module Mcp {
)
}

/** Holds if `handler` has an MCP `@tool`, `@prompt` or `@resource` decorator. */
private predicate hasMcpRegistrationDecorator(Function handler) {
exists(Expr dec | dec = handler.getADecorator() |
exists(API::Node server | server = serverClass().getAnInstance() |
dec = server.getMember(["tool", "prompt", "resource"]).getACall().asExpr()
or
dec = server.getMember(["tool", "prompt"]).getAValueReachableFromSource().asExpr()
)
or
exists(API::Node decorator | decorator = standaloneDecorator() |
dec = decorator.getACall().asExpr()
or
dec = decorator.getAValueReachableFromSource().asExpr()
)
)
}

/** Gets a reference to the `Context` class whose instances the SDKs inject into handlers. */
private API::Node contextClass() {
exists(API::Node mcpServer | mcpServer = API::moduleImport("mcp").getMember("server") |
Expand Down Expand Up @@ -278,7 +298,11 @@ module Mcp {
* parameter as well.
*/
private predicate isClientParameter(Function handler, Parameter p) {
handler = handlerNode().getAValueReachingSink().asExpr().(CallableExpr).getInnerScope() and
(
handler = handlerNode().getAValueReachingSink().asExpr().(CallableExpr).getInnerScope()
or
hasMcpRegistrationDecorator(handler)
) and
(
p = handler.getAnArg()
or
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import functools

from fastmcp import Context, FastMCP
from fastmcp.tools import tool as standalone_tool

app = FastMCP("wrapped-test")


def custom_wrapper(fn):
@functools.wraps(fn)
def wrapper(*args, **kwargs):
return fn(*args, **kwargs)

return wrapper


@app.tool()
@custom_wrapper
def wrapped_with_context(command: str, ctx: Context) -> str: # $ threatModelSource[ai]=command
return command


@standalone_tool
@custom_wrapper
def standalone_wrapped(command: str) -> str: # $ threatModelSource[ai]=command
return command


class WrappedHandlers:
@app.tool()
@custom_wrapper
def class_body_wrapped(command: str, ctx: Context) -> str: # $ threatModelSource[ai]=command
return command

@app.tool()
@staticmethod
def static_wrapped(command: str, ctx: Context) -> str: # $ threatModelSource[ai]=command
return command