Files
dmex 3bb9943917 Use monotonic timing for AgentTools timeouts
Standardize deadlines on PhQueryWaitTime with matching tick conversions and remove the local timing wrapper. Use unbiased interrupt time for pending consent expiry so wall-clock changes cannot affect validity.
2026-10-20 20:03:34 +11:00

1720 lines
50 KiB
C

/*
* Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved.
*
* This file is part of System Informer.
*
* Authors:
*
* jxy-s 2026
*
*/
#include "agenttools.h"
#define AT_MODERN_PROTOCOL_VERSION SIMCP_MODERN_PROTOCOL_VERSION
#define AT_META_PROTOCOL_VERSION "io.modelcontextprotocol/protocolVersion"
#define AT_META_CLIENT_INFO "io.modelcontextprotocol/clientInfo"
#define AT_META_CLIENT_CAPABILITIES "io.modelcontextprotocol/clientCapabilities"
#define AT_META_SERVER_INFO "io.modelcontextprotocol/serverInfo"
#define AT_META_SCHEMA_VERSION "dev.systeminformer/schema_version"
#define AT_JSONRPC_PARSE_ERROR (-32700)
#define AT_JSONRPC_INVALID_REQUEST (-32600)
#define AT_JSONRPC_METHOD_NOT_FOUND (-32601)
#define AT_JSONRPC_INVALID_PARAMS (-32602)
#define AT_JSONRPC_INTERNAL_ERROR (-32603)
#define AT_MCP_UNSUPPORTED_PROTOCOL_VERSION (-32022)
#define AT_WAIT_POLL_INTERVAL_MS 250
static PCSTR AtpLegacyProtocolVersions[] =
{
SIMCP_LEGACY_PROTOCOL_VERSIONS
};
static CONST CHAR AtpServerInstructions[] = SIMCP_SERVER_INSTRUCTIONS;
typedef enum _AT_INCOMING_RESULT
{
AtIncomingHandled,
AtIncomingCancelled,
AtIncomingResponse
} AT_INCOMING_RESULT;
typedef struct _AT_REQUEST_META
{
BOOLEAN Modern;
BOOLEAN Elicitation;
} AT_REQUEST_META, *PAT_REQUEST_META;
VOID AtJsonAddString(
_In_ PVOID Object,
_In_ PCSTR Key,
_In_opt_ PPH_STRING String
)
{
if (String)
AtJsonAddStringRef(Object, Key, &String->sr);
else
AtJsonAddNull(Object, Key);
}
VOID AtJsonAddStringRef(
_In_ PVOID Object,
_In_ PCSTR Key,
_In_opt_ PCPH_STRINGREF String
)
{
PPH_BYTES utf8;
if (!String || !String->Buffer)
{
AtJsonAddNull(Object, Key);
return;
}
if (!(utf8 = PhConvertUtf16ToUtf8Ex(String->Buffer, String->Length)))
{
AtJsonAddNull(Object, Key);
return;
}
PhAddJsonObjectUtf8(Object, Key, utf8);
PhDereferenceObject(utf8);
}
VOID AtJsonAddNull(
_In_ PVOID Object,
_In_ PCSTR Key
)
{
PhAddJsonObjectValue(Object, Key, NULL);
}
VOID AtJsonAddTime(
_In_ PVOID Object,
_In_ PCSTR Key,
_In_ PLARGE_INTEGER Time
)
{
SYSTEMTIME systemTime;
PPH_STRING string;
if (Time->QuadPart == 0)
{
AtJsonAddNull(Object, Key);
return;
}
PhLargeIntegerToSystemTime(&systemTime, Time);
string = PhFormatSystemTimeISO(&systemTime);
AtJsonAddString(Object, Key, string);
PhDereferenceObject(string);
}
PVOID AtJsonGetObjectMember(
_In_opt_ PVOID Object,
_In_ PCSTR Key,
_In_ PH_JSON_OBJECT_TYPE Type
)
{
PVOID member;
if (!Object)
return NULL;
member = PhGetJsonObject(Object, Key);
if (!member || PhGetJsonObjectType(member) != Type)
return NULL;
return member;
}
BOOLEAN AtJsonGetObjectBoolean(
_In_opt_ PVOID Object,
_In_ PCSTR Key
)
{
if (!AtJsonGetObjectMember(Object, Key, PH_JSON_OBJECT_TYPE_BOOLEAN))
return FALSE;
return !!PhGetJsonObjectBool(Object, Key);
}
PPH_BYTES AtpSerialize(
_In_ PVOID Object
)
{
return PhJsonObjectToJsonString(Object, PH_JSON_TO_STRING_PLAIN);
}
PVOID AtpParseLiteral(
_In_ PCSTR Literal
)
{
PVOID object;
if (NT_SUCCESS(PhCreateJsonParser(&object, Literal)))
return object;
return NULL;
}
BOOLEAN AtpEqualStringUtf8(
_In_opt_ PPH_STRING String,
_In_ PCSTR Literal
)
{
PH_BYTESREF literal;
PPH_STRING literalString;
BOOLEAN result;
if (!String)
return FALSE;
PhInitializeBytesRef(&literal, Literal);
literalString = PhZeroExtendToUtf16Ex(literal.Buffer, literal.Length);
result = PhEqualString(String, literalString, FALSE);
PhDereferenceObject(literalString);
return result;
}
PVOID AtpCreateServerInfo(
VOID
)
{
PVOID serverInfo;
PPH_STRING version;
serverInfo = PhCreateJsonObject();
PhAddJsonObject(serverInfo, "name", "SystemInformer");
PhAddJsonObject(serverInfo, "title", "System Informer");
if (version = PhGetBuildVersion())
{
AtJsonAddString(serverInfo, "version", version);
PhDereferenceObject(version);
}
else
{
PhAddJsonObject(serverInfo, "version", "0.0.0.0");
}
return serverInfo;
}
PVOID AtpCreateCapabilities(
VOID
)
{
PVOID capabilities;
PVOID tools;
PVOID resources;
PVOID prompts;
capabilities = PhCreateJsonObject();
// listChanged says the notification may arrive, not that this server is the one that sends it.
tools = PhCreateJsonObject();
PhAddJsonObjectBoolean(tools, "listChanged", TRUE);
PhAddJsonObjectValue(capabilities, "tools", tools);
resources = PhCreateJsonObject();
PhAddJsonObjectBoolean(resources, "listChanged", TRUE);
PhAddJsonObjectValue(capabilities, "resources", resources);
prompts = PhCreateJsonObject();
PhAddJsonObjectBoolean(prompts, "listChanged", TRUE);
PhAddJsonObjectValue(capabilities, "prompts", prompts);
return capabilities;
}
NTSTATUS AtpSendLine(
_In_ PAT_CONNECTION Connection,
_In_ PPH_BYTES Line
)
{
NTSTATUS status;
status = AtConnectionSend(Connection, SimcpMcp, Line->Buffer, (ULONG)Line->Length);
// A write that fails means the other end is gone or the pipe is broken, and nothing further
// will reach the client; the handshake treats the same failure the same way. Closing here
// stops the rest of this exchange being written into a pipe that cannot carry it.
if (!NT_SUCCESS(status))
AtConnectionClose(Connection, SimcpCloseTransportError, (ULONG)status);
return status;
}
VOID AtpSendError(
_In_ PAT_CONNECTION Connection,
_In_opt_ PPH_BYTES IdJson,
_In_ LONG Code,
_In_ PCSTR Message,
_In_opt_ PVOID Data
)
{
static CONST CHAR head[] = "{\"jsonrpc\":\"2.0\",\"id\":";
static CONST CHAR nullId[] = "null";
static CONST CHAR middle[] = ",\"error\":";
static CONST CHAR tail[] = "}";
PH_BYTES_BUILDER builder;
PVOID error;
PPH_BYTES errorJson;
PPH_BYTES line;
error = PhCreateJsonObject();
PhAddJsonObjectInt64(error, "code", Code);
PhAddJsonObject(error, "message", Message);
if (Data)
PhAddJsonObjectValue(error, "data", Data);
errorJson = AtpSerialize(error);
PhFreeJsonObject(error);
if (!errorJson)
return;
PhInitializeBytesBuilder(&builder, errorJson->Length + 64);
PhAppendBytesBuilderEx(&builder, (PVOID)head, sizeof(head) - 1, 0, NULL);
if (IdJson)
PhAppendBytesBuilderEx(&builder, IdJson->Buffer, IdJson->Length, 0, NULL);
else
PhAppendBytesBuilderEx(&builder, (PVOID)nullId, sizeof(nullId) - 1, 0, NULL);
PhAppendBytesBuilderEx(&builder, (PVOID)middle, sizeof(middle) - 1, 0, NULL);
PhAppendBytesBuilderEx(&builder, errorJson->Buffer, errorJson->Length, 0, NULL);
PhAppendBytesBuilderEx(&builder, (PVOID)tail, sizeof(tail) - 1, 0, NULL);
line = PhFinalBytesBuilderBytes(&builder);
AtpSendLine(Connection, line);
PhDereferenceObject(line);
PhDereferenceObject(errorJson);
}
VOID AtpSendResult(
_In_ PAT_CONNECTION Connection,
_In_ PPH_BYTES IdJson,
_In_ PVOID Result,
_In_ BOOLEAN Modern
)
{
static CONST CHAR head[] = "{\"jsonrpc\":\"2.0\",\"id\":";
static CONST CHAR middle[] = ",\"result\":";
static CONST CHAR tail[] = "}";
PH_BYTES_BUILDER builder;
PPH_BYTES resultJson;
PPH_BYTES line;
if (Modern)
{
PVOID meta;
if (!PhGetJsonObject(Result, "resultType"))
PhAddJsonObject(Result, "resultType", "complete");
if (!(meta = AtJsonGetObjectMember(Result, "_meta", PH_JSON_OBJECT_TYPE_OBJECT)))
{
meta = PhCreateJsonObject();
PhAddJsonObjectValue(Result, "_meta", meta);
}
PhAddJsonObjectValue(meta, AT_META_SERVER_INFO, AtpCreateServerInfo());
}
resultJson = AtpSerialize(Result);
PhFreeJsonObject(Result);
// The call has already run by now, so dropping the reply would leave the client believing a
// write it asked for never happened. It is told the answer was lost instead.
if (!resultJson)
{
AtpSendError(Connection, IdJson, AT_JSONRPC_INTERNAL_ERROR, "The result could not be serialized", NULL);
return;
}
PhInitializeBytesBuilder(&builder, resultJson->Length + IdJson->Length + 64);
PhAppendBytesBuilderEx(&builder, (PVOID)head, sizeof(head) - 1, 0, NULL);
PhAppendBytesBuilderEx(&builder, IdJson->Buffer, IdJson->Length, 0, NULL);
PhAppendBytesBuilderEx(&builder, (PVOID)middle, sizeof(middle) - 1, 0, NULL);
PhAppendBytesBuilderEx(&builder, resultJson->Buffer, resultJson->Length, 0, NULL);
PhAppendBytesBuilderEx(&builder, (PVOID)tail, sizeof(tail) - 1, 0, NULL);
line = PhFinalBytesBuilderBytes(&builder);
AtpSendLine(Connection, line);
PhDereferenceObject(line);
PhDereferenceObject(resultJson);
}
VOID AtpSendRequest(
_In_ PAT_CONNECTION Connection,
_In_ ULONG Id,
_In_ PCSTR Method,
_In_ PVOID Params
)
{
PVOID request;
PPH_BYTES line;
request = PhCreateJsonObject();
PhAddJsonObject(request, "jsonrpc", "2.0");
PhAddJsonObjectInt64(request, "id", Id);
PhAddJsonObject(request, "method", Method);
PhAddJsonObjectValue(request, "params", Params);
if (line = AtpSerialize(request))
{
AtpSendLine(Connection, line);
PhDereferenceObject(line);
}
PhFreeJsonObject(request);
}
#define AT_CLIENT_STRING_MAX_CHARS 256
PPH_STRING AtpSanitizeClientString(
_In_opt_ PPH_STRING String
)
{
PPH_STRING result;
SIZE_T count;
SIZE_T i;
SIZE_T visible = 0;
if (!String)
return NULL;
count = String->Length / sizeof(WCHAR);
if (count > AT_CLIENT_STRING_MAX_CHARS)
count = AT_CLIENT_STRING_MAX_CHARS;
// Do not cut a surrogate pair in half.
if (count && IS_HIGH_SURROGATE(String->Buffer[count - 1]))
count--;
// Fresh and unshared, so it may still be edited in place.
result = PhCreateStringEx(String->Buffer, count * sizeof(WCHAR));
PhDereferenceObject(String);
for (i = 0; i < count; i++)
{
if (AtIsUnsafeDisplayChar(result->Buffer[i]))
result->Buffer[i] = L' ';
else if (result->Buffer[i] != L' ')
visible++;
}
if (!visible)
{
PhDereferenceObject(result);
return NULL;
}
return result;
}
VOID AtpUpdateClientInfo(
_In_ PAT_CONNECTION Connection,
_In_opt_ PVOID ClientInfo
)
{
PPH_STRING name;
PPH_STRING version;
if (!ClientInfo)
return;
name = AtpSanitizeClientString(PhGetJsonValueAsString(ClientInfo, "name"));
version = AtpSanitizeClientString(PhGetJsonValueAsString(ClientInfo, "version"));
PhAcquireQueuedLockExclusive(&Connection->Lock);
PhMoveReference(&Connection->ClientName, name);
PhMoveReference(&Connection->ClientVersion, version);
PhReleaseQueuedLockExclusive(&Connection->Lock);
}
BOOLEAN AtpHasFormElicitation(
_In_opt_ PVOID Capabilities
)
{
PVOID elicitation;
if (!(elicitation = AtJsonGetObjectMember(Capabilities, "elicitation", PH_JSON_OBJECT_TYPE_OBJECT)))
return FALSE;
if (PhGetJsonObjectLength(elicitation) == 0)
return TRUE;
return !!PhGetJsonObject(elicitation, "form");
}
BOOLEAN AtpParseRequestMeta(
_In_ PAT_CONNECTION Connection,
_In_ PPH_BYTES IdJson,
_In_opt_ PVOID Params,
_Out_ PAT_REQUEST_META Meta
)
{
PVOID meta;
PPH_STRING version;
PVOID capabilities;
memset(Meta, 0, sizeof(AT_REQUEST_META));
if (!(meta = AtJsonGetObjectMember(Params, "_meta", PH_JSON_OBJECT_TYPE_OBJECT)))
return TRUE;
if (!(version = PhGetJsonValueAsString(meta, AT_META_PROTOCOL_VERSION)))
return TRUE; // legacy request that happens to carry _meta
Meta->Modern = TRUE;
if (!AtpEqualStringUtf8(version, AT_MODERN_PROTOCOL_VERSION))
{
PVOID data;
PVOID supported;
data = PhCreateJsonObject();
if (supported = AtpParseLiteral("[\"" AT_MODERN_PROTOCOL_VERSION "\"]"))
PhAddJsonObjectValue(data, "supported", supported);
AtJsonAddString(data, "requested", version);
AtpSendError(Connection, IdJson, AT_MCP_UNSUPPORTED_PROTOCOL_VERSION, "Unsupported protocol version", data);
PhDereferenceObject(version);
return FALSE;
}
PhDereferenceObject(version);
if (!(capabilities = AtJsonGetObjectMember(meta, AT_META_CLIENT_CAPABILITIES, PH_JSON_OBJECT_TYPE_OBJECT)))
{
AtpSendError(Connection, IdJson, AT_JSONRPC_INVALID_PARAMS, "Missing io.modelcontextprotocol/clientCapabilities", NULL);
return FALSE;
}
Meta->Elicitation = AtpHasFormElicitation(capabilities);
AtpUpdateClientInfo(Connection, AtJsonGetObjectMember(meta, AT_META_CLIENT_INFO, PH_JSON_OBJECT_TYPE_OBJECT));
return TRUE;
}
VOID AtpHandleInitialize(
_In_ PAT_CONNECTION Connection,
_In_ PPH_BYTES IdJson,
_In_opt_ PVOID Params
)
{
PPH_STRING requested;
PCSTR negotiated = AtpLegacyProtocolVersions[0];
PVOID result;
PVOID meta;
ULONG i;
if (requested = PhGetJsonValueAsString(Params, "protocolVersion"))
{
for (i = 0; i < RTL_NUMBER_OF(AtpLegacyProtocolVersions); i++)
{
if (AtpEqualStringUtf8(requested, AtpLegacyProtocolVersions[i]))
{
negotiated = AtpLegacyProtocolVersions[i];
break;
}
}
PhDereferenceObject(requested);
}
Connection->LegacyElicitation = AtpHasFormElicitation(AtJsonGetObjectMember(Params, "capabilities", PH_JSON_OBJECT_TYPE_OBJECT));
AtpUpdateClientInfo(Connection, AtJsonGetObjectMember(Params, "clientInfo", PH_JSON_OBJECT_TYPE_OBJECT));
PhAcquireQueuedLockExclusive(&Connection->Lock);
PhMoveReference(&Connection->ProtocolVersion, PhZeroExtendToUtf16(negotiated));
PhReleaseQueuedLockExclusive(&Connection->Lock);
result = PhCreateJsonObject();
PhAddJsonObject(result, "protocolVersion", negotiated);
PhAddJsonObjectValue(result, "capabilities", AtpCreateCapabilities());
PhAddJsonObjectValue(result, "serverInfo", AtpCreateServerInfo());
PhAddJsonObject(result, "instructions", AtpServerInstructions);
meta = PhCreateJsonObject();
PhAddJsonObjectInt64(meta, AT_META_SCHEMA_VERSION, AT_SCHEMA_VERSION);
PhAddJsonObjectValue(result, "_meta", meta);
AtpSendResult(Connection, IdJson, result, FALSE);
}
VOID AtpHandleDiscover(
_In_ PAT_CONNECTION Connection,
_In_ PPH_BYTES IdJson
)
{
PVOID result;
PVOID supported;
PVOID meta;
result = PhCreateJsonObject();
if (supported = AtpParseLiteral("[\"" AT_MODERN_PROTOCOL_VERSION "\"]"))
PhAddJsonObjectValue(result, "supportedVersions", supported);
PhAddJsonObjectValue(result, "capabilities", AtpCreateCapabilities());
PhAddJsonObject(result, "instructions", AtpServerInstructions);
meta = PhCreateJsonObject();
PhAddJsonObjectInt64(meta, AT_META_SCHEMA_VERSION, AT_SCHEMA_VERSION);
PhAddJsonObjectValue(result, "_meta", meta);
AtpSendResult(Connection, IdJson, result, TRUE);
}
VOID AtpHandleToolsList(
_In_ PAT_CONNECTION Connection,
_In_ PPH_BYTES IdJson,
_In_ BOOLEAN Modern
)
{
PVOID result;
PVOID tools;
result = PhCreateJsonObject();
tools = PhCreateJsonArray();
AtEnumTools(tools);
PhAddJsonObjectValue(result, "tools", tools);
AtpSendResult(Connection, IdJson, result, Modern);
}
VOID AtpHandlePromptsList(
_In_ PAT_CONNECTION Connection,
_In_ PPH_BYTES IdJson,
_In_ BOOLEAN Modern
)
{
PVOID result;
PVOID prompts;
result = PhCreateJsonObject();
prompts = PhCreateJsonArray();
AtEnumPrompts(prompts);
PhAddJsonObjectValue(result, "prompts", prompts);
AtpSendResult(Connection, IdJson, result, Modern);
}
PPH_BYTES AtpFormatPrompt(
_In_ PCAT_PROMPT Prompt,
_In_opt_ PVOID Arguments
)
{
PPH_STRING text;
PPH_STRING placeholder;
PPH_STRING value = NULL;
PH_STRINGREF before;
PH_STRINGREF after;
PPH_BYTES bytes;
text = PhZeroExtendToUtf16(Prompt->Text);
if (!Prompt->Argument)
{
bytes = PhConvertUtf16ToUtf8Ex(text->Buffer, text->Length);
PhDereferenceObject(text);
return bytes;
}
if (Arguments)
value = PhGetJsonValueAsString(Arguments, Prompt->Argument);
if (PhIsNullOrEmptyString(value))
PhMoveReference(&value, PhZeroExtendToUtf16(Prompt->Fallback));
placeholder = PhFormatString(L"{%hs}", Prompt->Argument);
if (PhSplitStringRefAtString(&text->sr, &placeholder->sr, FALSE, &before, &after))
{
PPH_STRING filled;
filled = PhConcatStringRef3(&before, &value->sr, &after);
PhMoveReference(&text, filled);
}
PhDereferenceObject(placeholder);
PhClearReference(&value);
bytes = PhConvertUtf16ToUtf8Ex(text->Buffer, text->Length);
PhDereferenceObject(text);
return bytes;
}
VOID AtpHandlePromptsGet(
_In_ PAT_CONNECTION Connection,
_In_ PPH_BYTES IdJson,
_In_opt_ PVOID Params,
_In_ BOOLEAN Modern
)
{
PPH_STRING name;
PCAT_PROMPT prompt;
PPH_BYTES text;
PVOID result;
PVOID messages;
PVOID message;
PVOID content;
if (!(name = PhGetJsonValueAsString(Params, "name")))
{
AtpSendError(Connection, IdJson, AT_JSONRPC_INVALID_PARAMS, "Missing prompt name", NULL);
return;
}
prompt = AtFindPrompt(name);
PhDereferenceObject(name);
if (!prompt)
{
AtpSendError(Connection, IdJson, AT_JSONRPC_INVALID_PARAMS, "Unknown prompt", NULL);
return;
}
if (!(text = AtpFormatPrompt(prompt, AtJsonGetObjectMember(Params, "arguments", PH_JSON_OBJECT_TYPE_OBJECT))))
{
AtpSendError(Connection, IdJson, AT_JSONRPC_INTERNAL_ERROR, "The prompt could not be built", NULL);
return;
}
content = PhCreateJsonObject();
PhAddJsonObject(content, "type", "text");
PhAddJsonObjectUtf8(content, "text", text);
message = PhCreateJsonObject();
PhAddJsonObject(message, "role", "user");
PhAddJsonObjectValue(message, "content", content);
messages = PhCreateJsonArray();
PhAddJsonArrayObject(messages, message);
result = PhCreateJsonObject();
PhAddJsonObjectValue(result, "messages", messages);
AtpSendResult(Connection, IdJson, result, Modern);
PhDereferenceObject(text);
}
VOID AtpHandleResourcesList(
_In_ PAT_CONNECTION Connection,
_In_ PPH_BYTES IdJson,
_In_ BOOLEAN Modern
)
{
PVOID result;
PVOID resources;
result = PhCreateJsonObject();
resources = PhCreateJsonArray();
AtEnumResources(resources);
PhAddJsonObjectValue(result, "resources", resources);
AtpSendResult(Connection, IdJson, result, Modern);
}
PVOID AtpCreateTextContent(
_In_ PPH_BYTES Text
)
{
PVOID content;
PVOID item;
content = PhCreateJsonArray();
item = PhCreateJsonObject();
PhAddJsonObject(item, "type", "text");
PhAddJsonObjectUtf8(item, "text", Text);
PhAddJsonArrayObject(content, item);
return content;
}
VOID AtpSendToolResult(
_In_ PAT_TOOL_CALL Call,
_Inout_ PAT_TOOL_RESULT ToolResult
)
{
PVOID result;
// A handler that set neither an error nor an answer took a path that forgot to set one.
// Publishing the empty object would present that as a successful empty result, so it is
// reported as the internal error it is instead.
if (!ToolResult->ErrorCode && !ToolResult->StructuredContent)
{
NT_ASSERT(FALSE);
AtSetToolError(ToolResult, "internal_error", STATUS_INTERNAL_ERROR,
L"The tool returned neither a result nor an error.");
}
result = PhCreateJsonObject();
if (ToolResult->ErrorCode)
{
PVOID error;
PPH_BYTES text;
// isError plus a structured description in the text block, and no structuredContent, so
// output schema validation is not tripped by the error shape.
error = PhCreateJsonObject();
PhAddJsonObject(error, "error", ToolResult->ErrorCode);
AtJsonAddString(error, "message", ToolResult->ErrorMessage);
if (!NT_SUCCESS(ToolResult->Status))
PhAddJsonObjectInt64(error, "ntstatus", (LONG)ToolResult->Status);
AtAddErrorHints(error, ToolResult);
if (text = AtpSerialize(error))
{
PhAddJsonObjectValue(result, "content", AtpCreateTextContent(text));
PhDereferenceObject(text);
}
PhFreeJsonObject(error);
PhAddJsonObjectBoolean(result, "isError", TRUE);
}
else
{
PPH_BYTES text;
if (!ToolResult->StructuredContent)
ToolResult->StructuredContent = PhCreateJsonObject();
if (text = AtpSerialize(ToolResult->StructuredContent))
{
PhAddJsonObjectValue(result, "content", AtpCreateTextContent(text));
PhDereferenceObject(text);
}
PhAddJsonObjectValue(result, "structuredContent", ToolResult->StructuredContent);
ToolResult->StructuredContent = NULL;
PhAddJsonObjectBoolean(result, "isError", FALSE);
}
AtpSendResult(Call->Connection, Call->IdJson, result, Call->Modern);
}
VOID AtpHandleToolsCall(
_In_ PAT_CONNECTION Connection,
_In_ PPH_BYTES IdJson,
_In_opt_ PVOID Params,
_In_ PAT_REQUEST_META Meta
)
{
PPH_STRING name;
PCAT_TOOL tool;
AT_TOOL_CALL call;
AT_TOOL_RESULT result;
AT_TARGET target;
BOOLEAN sendResult = TRUE;
if (!(name = PhGetJsonValueAsString(Params, "name")))
{
AtpSendError(Connection, IdJson, AT_JSONRPC_INVALID_PARAMS, "Missing tool name", NULL);
return;
}
tool = AtFindTool(name);
PhDereferenceObject(name);
if (!tool)
{
AtpSendError(Connection, IdJson, AT_JSONRPC_INVALID_PARAMS, "Unknown tool", NULL);
return;
}
memset(&call, 0, sizeof(AT_TOOL_CALL));
call.Connection = Connection;
call.IdJson = IdJson;
call.Modern = Meta->Modern;
call.ClientElicitation = Meta->Modern ? Meta->Elicitation : Connection->LegacyElicitation;
call.Arguments = AtJsonGetObjectMember(Params, "arguments", PH_JSON_OBJECT_TYPE_OBJECT);
call.RequestState = PhGetJsonValueAsString(Params, "requestState");
call.InputResponses = AtJsonGetObjectMember(Params, "inputResponses", PH_JSON_OBJECT_TYPE_OBJECT);
memset(&result, 0, sizeof(AT_TOOL_RESULT));
memset(&target, 0, sizeof(AT_TARGET));
PhAcquireQueuedLockExclusive(&Connection->Lock);
PhMoveReference(&Connection->InFlightId, PhReferenceObject(IdJson));
Connection->InFlightCancelled = FALSE;
Connection->CallCount++;
PhReleaseQueuedLockExclusive(&Connection->Lock);
if (AtConsentWaitForConnection(Connection) != AtConsentAllowed)
{
sendResult = FALSE;
}
else if (!AtIsToolEnabled(tool))
{
AtSetToolError(&result, "disabled", STATUS_SUCCESS, L"This tool is disabled in System Informer's AgentTools options.");
}
else
{
BOOLEAN gate;
// Reads resolve their own target; other tiers resolve first and pass one only if there is one.
if (tool->Tier == AtTierRead)
gate = TRUE;
else
gate = NT_SUCCESS(AtResolveTarget(tool, call.Arguments, &target, &result));
if (gate)
{
switch (AtConsentGate(&call, &AtActionInfo[tool->Action], target.Kind != AtTargetNone ? &target : NULL))
{
case AtConsentAllowed:
break;
case AtConsentInputRequired:
sendResult = FALSE;
break;
case AtConsentCancelled:
sendResult = FALSE;
break;
case AtConsentDenied:
AtSetToolError(&result, "consent_denied", STATUS_SUCCESS, L"The user denied this request.");
break;
case AtConsentTimeout:
AtSetToolError(&result, "consent_timeout", STATUS_SUCCESS, L"The user did not answer the confirmation in time; the request was denied.");
break;
case AtConsentDeclined:
AtSetToolError(&result, "consent_declined", STATUS_SUCCESS, L"The confirmation was declined or cancelled.");
break;
case AtConsentElicitationRequired:
AtSetToolError(&result, "elicitation_required", STATUS_SUCCESS, L"System Informer is configured to delegate confirmation to the client, but this client does not support elicitation. Enable confirmation in System Informer's AgentTools options or use a client with elicitation support.");
break;
default:
AtSetToolError(&result, "consent_failed", STATUS_SUCCESS, L"The confirmation could not be completed.");
break;
}
}
}
if (sendResult)
{
if (!result.ErrorCode)
AtInvokeTool(tool, &call, &target, &result);
AtpSendToolResult(&call, &result);
}
AtDeleteTarget(&target);
AtDeleteToolResult(&result);
PhClearReference(&call.RequestState);
PhAcquireQueuedLockExclusive(&Connection->Lock);
PhClearReference(&Connection->InFlightId);
PhReleaseQueuedLockExclusive(&Connection->Lock);
}
VOID AtpHandleResourcesRead(
_In_ PAT_CONNECTION Connection,
_In_ PPH_BYTES IdJson,
_In_opt_ PVOID Params,
_In_ PAT_REQUEST_META Meta
)
{
PPH_STRING uri;
PPH_STRING toolName;
PCAT_RESOURCE resource;
PCAT_TOOL tool;
AT_TOOL_CALL call;
AT_TOOL_RESULT result;
AT_TARGET target;
PVOID arguments = NULL;
PPH_BYTES text = NULL;
if (!(uri = PhGetJsonValueAsString(Params, "uri")))
{
AtpSendError(Connection, IdJson, AT_JSONRPC_INVALID_PARAMS, "Missing resource uri", NULL);
return;
}
resource = AtFindResource(uri);
if (!resource)
{
AtpSendError(Connection, IdJson, AT_JSONRPC_INVALID_PARAMS, "Unknown resource", NULL);
PhDereferenceObject(uri);
return;
}
toolName = PhZeroExtendToUtf16(resource->ToolName);
tool = AtFindTool(toolName);
PhDereferenceObject(toolName);
if (!tool)
{
NT_ASSERT(FALSE); // a resource in schema.c names a tool that is not there
AtpSendError(Connection, IdJson, AT_JSONRPC_INTERNAL_ERROR, "Unknown resource", NULL);
PhDereferenceObject(uri);
return;
}
if (!AtIsToolEnabled(tool))
{
AtpSendError(Connection, IdJson, AT_JSONRPC_INVALID_PARAMS,
"The tool behind this resource is disabled in System Informer's AgentTools options", NULL);
PhDereferenceObject(uri);
return;
}
if (!NT_SUCCESS(PhCreateJsonParser(&arguments, resource->Arguments)))
{
NT_ASSERT(FALSE); // arguments in schema.c do not parse
AtpSendError(Connection, IdJson, AT_JSONRPC_INTERNAL_ERROR, "Resource arguments could not be read", NULL);
PhDereferenceObject(uri);
return;
}
memset(&call, 0, sizeof(AT_TOOL_CALL));
call.Connection = Connection;
call.IdJson = IdJson;
call.Modern = Meta->Modern;
call.ClientElicitation = Meta->Modern ? Meta->Elicitation : Connection->LegacyElicitation;
call.Arguments = arguments;
memset(&result, 0, sizeof(AT_TOOL_RESULT));
memset(&target, 0, sizeof(AT_TARGET));
PhAcquireQueuedLockExclusive(&Connection->Lock);
PhMoveReference(&Connection->InFlightId, PhReferenceObject(IdJson));
Connection->InFlightCancelled = FALSE;
Connection->CallCount++;
PhReleaseQueuedLockExclusive(&Connection->Lock);
// Every resource is backed by a read, which carries no target through the gate.
NT_ASSERT(tool->Tier == AtTierRead);
if (AtConsentWaitForConnection(Connection) != AtConsentAllowed ||
AtConsentGate(&call, &AtActionInfo[tool->Action], NULL) != AtConsentAllowed)
{
// A resource read has nowhere to put a consent conversation - there is no isError shape
// for it - so a refusal is a protocol error naming what happened.
AtpSendError(Connection, IdJson, AT_JSONRPC_INVALID_PARAMS,
"The user did not allow this resource to be read", NULL);
}
else
{
AtInvokeTool(tool, &call, &target, &result);
if (result.ErrorCode)
{
PPH_BYTES message = NULL;
// The tool's own message, not just its code: a resource read has no isError shape to
// carry the detail, so it goes in the protocol error or it is lost.
if (result.ErrorMessage)
message = PhConvertUtf16ToUtf8Ex(result.ErrorMessage->Buffer, result.ErrorMessage->Length);
AtpSendError(Connection, IdJson, AT_JSONRPC_INTERNAL_ERROR,
message ? message->Buffer : result.ErrorCode, NULL);
PhClearReference(&message);
}
else
{
if (!result.StructuredContent)
result.StructuredContent = PhCreateJsonObject();
if (text = AtpSerialize(result.StructuredContent))
{
PVOID contents;
PVOID entry;
PVOID root;
entry = PhCreateJsonObject();
PhAddJsonObject(entry, "uri", resource->Uri);
PhAddJsonObject(entry, "mimeType", "application/json");
PhAddJsonObjectUtf8(entry, "text", text);
contents = PhCreateJsonArray();
PhAddJsonArrayObject(contents, entry);
root = PhCreateJsonObject();
PhAddJsonObjectValue(root, "contents", contents);
AtpSendResult(Connection, IdJson, root, Meta->Modern);
PhDereferenceObject(text);
}
else
{
AtpSendError(Connection, IdJson, AT_JSONRPC_INTERNAL_ERROR, "The resource could not be serialized", NULL);
}
}
}
AtDeleteTarget(&target);
AtDeleteToolResult(&result);
PhFreeJsonObject(arguments);
PhDereferenceObject(uri);
PhAcquireQueuedLockExclusive(&Connection->Lock);
PhClearReference(&Connection->InFlightId);
PhReleaseQueuedLockExclusive(&Connection->Lock);
}
BOOLEAN AtpDeferRequest(
_In_ PAT_CONNECTION Connection,
_In_ PPH_BYTES IdJson,
_In_reads_bytes_(Length) PVOID Payload,
_In_ ULONG Length
)
{
PAT_DEFERRED_REQUEST request;
// A bounded queue: a client cannot grow the process by streaming requests into a wait.
if (Connection->DeferredCount >= AT_MAX_DEFERRED_REQUESTS ||
Connection->DeferredBytes + Length > AT_MAX_DEFERRED_BYTES)
{
return FALSE;
}
request = PhAllocate(UFIELD_OFFSET(AT_DEFERRED_REQUEST, Payload) + Length);
request->IdJson = PhReferenceObject(IdJson);
request->Length = Length;
memcpy(request->Payload, Payload, Length);
InsertTailList(&Connection->DeferredRequests, &request->ListEntry);
Connection->DeferredCount++;
Connection->DeferredBytes += Length;
return TRUE;
}
VOID AtpFreeDeferredRequest(
_In_ PAT_CONNECTION Connection,
_In_ PAT_DEFERRED_REQUEST Request
)
{
Connection->DeferredCount--;
Connection->DeferredBytes -= Request->Length;
PhDereferenceObject(Request->IdJson);
PhFree(Request);
}
VOID AtpHandleNotification(
_In_ PAT_CONNECTION Connection,
_In_ PPH_STRING Method,
_In_opt_ PVOID Params,
_Out_ PBOOLEAN Cancelled
)
{
*Cancelled = FALSE;
if (AtpEqualStringUtf8(Method, "notifications/initialized"))
{
Connection->Initialized = TRUE;
}
else if (AtpEqualStringUtf8(Method, "notifications/cancelled"))
{
PVOID requestId;
PPH_BYTES requestIdJson;
PLIST_ENTRY entry;
if (Params && (requestId = PhGetJsonObject(Params, "requestId")))
{
if (requestIdJson = PhGetJsonArrayString(requestId, FALSE))
{
PhAcquireQueuedLockExclusive(&Connection->Lock);
if (Connection->InFlightId &&
Connection->InFlightId->Length == requestIdJson->Length &&
memcmp(Connection->InFlightId->Buffer, requestIdJson->Buffer, requestIdJson->Length) == 0)
{
Connection->InFlightCancelled = TRUE;
*Cancelled = TRUE;
}
PhReleaseQueuedLockExclusive(&Connection->Lock);
// A request still queued behind a consent wait is dropped without a response.
for (entry = Connection->DeferredRequests.Flink; entry != &Connection->DeferredRequests; )
{
PAT_DEFERRED_REQUEST deferred = CONTAINING_RECORD(entry, AT_DEFERRED_REQUEST, ListEntry);
entry = entry->Flink;
if (deferred->IdJson->Length == requestIdJson->Length &&
memcmp(deferred->IdJson->Buffer, requestIdJson->Buffer, requestIdJson->Length) == 0)
{
RemoveEntryList(&deferred->ListEntry);
AtpFreeDeferredRequest(Connection, deferred);
}
}
PhDereferenceObject(requestIdJson);
}
}
}
}
AT_INCOMING_RESULT AtpProcessIncoming(
_In_ PAT_CONNECTION Connection,
_In_reads_bytes_(Length) PVOID Payload,
_In_ ULONG Length,
_In_ BOOLEAN DuringWait,
_In_ ULONG WaitResponseId,
_Outptr_opt_result_maybenull_ PVOID* Response
)
{
NTSTATUS status;
PPH_BYTES bytes;
PVOID message = NULL;
PPH_STRING method = NULL;
PVOID idObject;
PVOID params;
PPH_BYTES idJson = NULL;
AT_INCOMING_RESULT result = AtIncomingHandled;
if (Response)
*Response = NULL;
bytes = PhCreateBytesEx(Payload, Length);
status = PhCreateJsonParserEx(&message, bytes, FALSE);
PhDereferenceObject(bytes);
if (!NT_SUCCESS(status) || !message)
{
AtpSendError(Connection, NULL, AT_JSONRPC_PARSE_ERROR, "Parse error", NULL);
return AtIncomingHandled;
}
if (PhGetJsonObjectType(message) != PH_JSON_OBJECT_TYPE_OBJECT)
{
AtpSendError(Connection, NULL, AT_JSONRPC_INVALID_REQUEST, "Invalid request", NULL);
PhFreeJsonObject(message);
return AtIncomingHandled;
}
method = PhGetJsonValueAsString(message, "method");
idObject = PhGetJsonObject(message, "id");
params = AtJsonGetObjectMember(message, "params", PH_JSON_OBJECT_TYPE_OBJECT);
if (idObject && PhGetJsonObjectType(idObject) == PH_JSON_OBJECT_TYPE_NULL)
idObject = NULL;
if (!method)
{
// A response to one of our requests.
if (DuringWait && Response && idObject &&
PhGetJsonObjectType(idObject) == PH_JSON_OBJECT_TYPE_INT &&
PhGetJsonInt64Object(idObject) == WaitResponseId)
{
*Response = message;
return AtIncomingResponse;
}
PhFreeJsonObject(message);
return AtIncomingHandled;
}
if (!idObject)
{
BOOLEAN cancelled;
AtpHandleNotification(Connection, method, params, &cancelled);
if (cancelled && DuringWait)
result = AtIncomingCancelled;
goto CleanupExit;
}
if (!(idJson = PhGetJsonArrayString(idObject, FALSE)))
goto CleanupExit;
{
AT_REQUEST_META meta;
if (!AtpParseRequestMeta(Connection, idJson, params, &meta))
goto CleanupExit;
if (AtpEqualStringUtf8(method, "ping"))
{
AtpSendResult(Connection, idJson, PhCreateJsonObject(), meta.Modern);
}
else if (AtpEqualStringUtf8(method, "tools/list"))
{
AtpHandleToolsList(Connection, idJson, meta.Modern);
}
else if (AtpEqualStringUtf8(method, "resources/list"))
{
AtpHandleResourcesList(Connection, idJson, meta.Modern);
}
else if (AtpEqualStringUtf8(method, "prompts/list"))
{
AtpHandlePromptsList(Connection, idJson, meta.Modern);
}
else if (AtpEqualStringUtf8(method, "prompts/get"))
{
// A prompt is text: no gate, no tool, nothing to defer behind a consent wait.
AtpHandlePromptsGet(Connection, idJson, params, meta.Modern);
}
else if (AtpEqualStringUtf8(method, "resources/templates/list"))
{
// No templated resources: the answer is an empty list rather than a method the client
// has to discover is missing.
PVOID templates = PhCreateJsonObject();
PhAddJsonObjectValue(templates, "resourceTemplates", PhCreateJsonArray());
AtpSendResult(Connection, idJson, templates, meta.Modern);
}
else if (AtpEqualStringUtf8(method, "server/discover"))
{
if (meta.Modern)
AtpHandleDiscover(Connection, idJson);
else
AtpSendError(Connection, idJson, AT_JSONRPC_INVALID_PARAMS, "Missing io.modelcontextprotocol/protocolVersion", NULL);
}
else if (DuringWait)
{
// Behind the call that is waiting for consent; it runs once that call has finished.
if (!AtpDeferRequest(Connection, idJson, Payload, Length))
AtpSendError(Connection, idJson, AT_JSONRPC_INTERNAL_ERROR, "Too many requests are queued behind a confirmation on this connection", NULL);
}
else if (AtpEqualStringUtf8(method, "initialize"))
{
AtpHandleInitialize(Connection, idJson, params);
}
else if (AtpEqualStringUtf8(method, "tools/call"))
{
AtpHandleToolsCall(Connection, idJson, params, &meta);
}
else if (AtpEqualStringUtf8(method, "resources/read"))
{
AtpHandleResourcesRead(Connection, idJson, params, &meta);
}
else
{
AtpSendError(Connection, idJson, AT_JSONRPC_METHOD_NOT_FOUND, "Method not found", NULL);
}
}
CleanupExit:
if (idJson)
PhDereferenceObject(idJson);
if (method)
PhDereferenceObject(method);
PhFreeJsonObject(message);
return result;
}
VOID AtMcpHandleMessage(
_In_ PAT_CONNECTION Connection,
_In_reads_bytes_(Length) PVOID Payload,
_In_ ULONG Length
)
{
AtpProcessIncoming(Connection, Payload, Length, FALSE, 0, NULL);
// Requests that arrived during a consent wait, in order. One of them may wait in turn and
// queue more behind it.
while (!AtConnectionIsClosing(Connection) && !IsListEmpty(&Connection->DeferredRequests))
{
PAT_DEFERRED_REQUEST request;
request = CONTAINING_RECORD(RemoveHeadList(&Connection->DeferredRequests), AT_DEFERRED_REQUEST, ListEntry);
AtpProcessIncoming(Connection, request->Payload, request->Length, FALSE, 0, NULL);
AtpFreeDeferredRequest(Connection, request);
}
}
VOID AtMcpDeleteConnectionState(
_In_ PAT_CONNECTION Connection
)
{
while (!IsListEmpty(&Connection->DeferredRequests))
{
PAT_DEFERRED_REQUEST request;
request = CONTAINING_RECORD(RemoveHeadList(&Connection->DeferredRequests), AT_DEFERRED_REQUEST, ListEntry);
AtpFreeDeferredRequest(Connection, request);
}
}
BOOLEAN AtMcpPumpDuringWait(
_In_ PAT_CONNECTION Connection
)
{
while (!AtConnectionIsClosing(Connection))
{
NTSTATUS status;
BOOLEAN available = FALSE;
SIMCP_HEADER header;
PVOID payload;
AT_INCOMING_RESULT result;
if (!NT_SUCCESS(AtConnectionPeek(Connection, &available)))
return TRUE;
if (!available)
return FALSE;
status = AtConnectionRead(Connection, &header, &payload);
if (!NT_SUCCESS(status))
{
if (status == STATUS_INVALID_NETWORK_RESPONSE)
AtConnectionClose(Connection, SimcpCloseProtocolViolation, 0);
return TRUE;
}
if (header.Type != SimcpMcp)
{
if (payload)
PhFree(payload);
AtConnectionClose(Connection, SimcpCloseProtocolViolation, 0);
return FALSE;
}
if (!payload)
continue;
result = AtpProcessIncoming(Connection, payload, header.PayloadLength, TRUE, 0, NULL);
PhFree(payload);
if (result == AtIncomingCancelled)
return TRUE;
}
return TRUE;
}
PVOID AtpCreateElicitationParams(
_In_ PAT_TOOL_CALL Call,
_In_ PCAT_ACTION_INFO Action,
_In_opt_ PAT_TARGET Target,
_In_ BOOLEAN IncludeMode
)
{
PVOID params;
PVOID schema;
PVOID properties;
PVOID confirm;
PVOID required;
PPH_STRING target;
PPH_STRING caller;
PPH_STRING message;
target = Target ? AtFormatTargetDescription(Target) : NULL;
caller = AtFormatCallerDescription(Call->Connection);
if (Action->Tier == AtTierWrite)
{
message = PhFormatString(
L"System Informer: allow the connected agent to %s%s%s?\n\n%s\n\nRequested by: %s\n\nThis request was made by an AI agent through System Informer. Confirm only if you intended it.",
Action->Verb,
Target && Target->Parameter ? L" to " : L"",
Target && Target->Parameter ? PhGetString(Target->Parameter) : L"",
PhGetStringOrDefault(target, L"(no target)"),
PhGetString(caller)
);
}
else if (Action->Tier == AtTierSensitiveRead)
{
message = PhFormatString(
L"System Informer: allow the connected agent to %s for the rest of this session?\n\nFirst target: %s\n\nRequested by: %s\n\nConfirm only if you intended it.",
Action->Verb,
PhGetStringOrDefault(target, L"(none)"),
PhGetString(caller)
);
}
else if (Action->Tier == AtTierNetworkEgress)
{
message = PhFormatString(
L"System Informer: allow the connected agent to %s%s%s?\n\nRequested by: %s\n\nThis sends the request off this machine to a service on the internet. Confirm only if you intended it.",
Action->Verb,
Target && Target->Parameter ? L" " : L"",
Target && Target->Parameter ? PhGetString(Target->Parameter) : L"",
PhGetString(caller)
);
}
else
{
message = PhFormatString(
L"System Informer: allow the connected agent to %s for the rest of this session?\n\nRequested by: %s\n\nConfirm only if you intended it.",
Action->Verb,
PhGetString(caller)
);
}
params = PhCreateJsonObject();
if (IncludeMode)
PhAddJsonObject(params, "mode", "form");
AtJsonAddString(params, "message", message);
schema = PhCreateJsonObject();
PhAddJsonObject(schema, "type", "object");
properties = PhCreateJsonObject();
confirm = PhCreateJsonObject();
PhAddJsonObject(confirm, "type", "boolean");
PhAddJsonObject(confirm, "title", "Allow");
PhAddJsonObject(confirm, "description", "Set to true to allow the action described in the message.");
PhAddJsonObjectBoolean(confirm, "default", FALSE);
PhAddJsonObjectValue(properties, "confirm", confirm);
PhAddJsonObjectValue(schema, "properties", properties);
if (required = AtpParseLiteral("[\"confirm\"]"))
PhAddJsonObjectValue(schema, "required", required);
PhAddJsonObjectValue(params, "requestedSchema", schema);
PhDereferenceObject(message);
PhDereferenceObject(caller);
PhClearReference(&target);
return params;
}
AT_CONSENT_RESULT AtpInterpretElicitResult(
_In_opt_ PVOID ElicitResult
)
{
PPH_STRING action;
PVOID content;
AT_CONSENT_RESULT result = AtConsentDeclined;
if (!ElicitResult)
return AtConsentFailed;
if (action = PhGetJsonValueAsString(ElicitResult, "action"))
{
if (AtpEqualStringUtf8(action, "accept"))
{
if ((content = AtJsonGetObjectMember(ElicitResult, "content", PH_JSON_OBJECT_TYPE_OBJECT)) &&
AtJsonGetObjectBoolean(content, "confirm"))
{
result = AtConsentAllowed;
}
}
PhDereferenceObject(action);
}
return result;
}
AT_CONSENT_RESULT AtpElicitLegacy(
_In_ PAT_TOOL_CALL Call,
_In_ PCAT_ACTION_INFO Action,
_In_opt_ PAT_TARGET Target
)
{
PAT_CONNECTION connection = Call->Connection;
ULONG requestId;
BOOLEAN includeMode;
ULONG64 startTick;
AT_CONSENT_RESULT result = AtConsentFailed;
requestId = ++connection->NextServerRequestId;
includeMode = connection->ProtocolVersion && !AtpEqualStringUtf8(connection->ProtocolVersion, "2025-06-18") &&
!AtpEqualStringUtf8(connection->ProtocolVersion, "2025-03-26") && !AtpEqualStringUtf8(connection->ProtocolVersion, "2024-11-05");
AtpSendRequest(connection, requestId, "elicitation/create", AtpCreateElicitationParams(Call, Action, Target, includeMode));
startTick = PhQueryWaitTime();
while (!AtConnectionIsClosing(connection))
{
BOOLEAN available = FALSE;
SIMCP_HEADER header;
PVOID payload;
PVOID response = NULL;
AT_INCOMING_RESULT incoming;
if (PhQueryWaitTime() - startTick > UInt32x32To64(AT_ELICITATION_TIMEOUT_MS, PH_TIMEOUT_MS))
return AtConsentTimeout;
if (!NT_SUCCESS(AtConnectionPeek(connection, &available)))
return AtConsentFailed;
if (!available)
{
PhDelayExecution(AT_WAIT_POLL_INTERVAL_MS);
continue;
}
if (!NT_SUCCESS(AtConnectionRead(connection, &header, &payload)))
return AtConsentFailed;
if (header.Type != SimcpMcp)
{
if (payload)
PhFree(payload);
AtConnectionClose(connection, SimcpCloseProtocolViolation, 0);
return AtConsentFailed;
}
if (!payload)
continue;
incoming = AtpProcessIncoming(connection, payload, header.PayloadLength, TRUE, requestId, &response);
PhFree(payload);
if (incoming == AtIncomingCancelled)
return AtConsentCancelled;
if (incoming == AtIncomingResponse)
{
PVOID elicitResult;
elicitResult = AtJsonGetObjectMember(response, "result", PH_JSON_OBJECT_TYPE_OBJECT);
result = AtpInterpretElicitResult(elicitResult);
PhFreeJsonObject(response);
return result;
}
}
return AtConsentFailed;
}
PAT_PENDING_CONSENT AtpFindPendingConsent(
_In_ PAT_CONNECTION Connection,
_In_ PPH_STRING RequestState
)
{
ULONG64 nonce;
ULONG i;
if (!PhStringToUInt64(&RequestState->sr, 16, &nonce))
return NULL;
for (i = 0; i < AT_MAX_PENDING_CONSENTS; i++)
{
if (Connection->Pending[i].Used && Connection->Pending[i].Nonce == nonce)
return &Connection->Pending[i];
}
return NULL;
}
PAT_PENDING_CONSENT AtpAllocatePendingConsent(
_In_ PAT_CONNECTION Connection
)
{
LARGE_INTEGER now;
PAT_PENDING_CONSENT oldest = NULL;
ULONG i;
PhQueryUnbiasedInterruptTime(&now);
for (i = 0; i < AT_MAX_PENDING_CONSENTS; i++)
{
PAT_PENDING_CONSENT pending = &Connection->Pending[i];
if (!pending->Used || pending->Expiry.QuadPart < now.QuadPart)
return pending;
if (!oldest || pending->Expiry.QuadPart < oldest->Expiry.QuadPart)
oldest = pending;
}
return oldest;
}
AT_CONSENT_RESULT AtpElicitModern(
_In_ PAT_TOOL_CALL Call,
_In_ PCAT_ACTION_INFO Action,
_In_opt_ PAT_TARGET Target
)
{
PAT_CONNECTION connection = Call->Connection;
PAT_PENDING_CONSENT pending;
PVOID result;
PVOID inputRequests;
PVOID request;
LARGE_INTEGER now;
PH_FORMAT format[1];
PPH_STRING requestState;
ULONG64 identity[4] = { 0 };
if (Target)
memcpy(identity, Target->Identity, sizeof(identity));
// A retry that carries our state and the client's answer.
if (Call->RequestState && (pending = AtpFindPendingConsent(connection, Call->RequestState)))
{
BOOLEAN matches;
PhQueryUnbiasedInterruptTime(&now);
// Bind the state to this action and this exact target; consume it whatever the answer.
matches =
pending->Expiry.QuadPart >= now.QuadPart &&
pending->Action == Action->Action &&
memcmp(pending->Identity, identity, sizeof(identity)) == 0;
pending->Used = FALSE;
if (matches && Call->InputResponses)
{
return AtpInterpretElicitResult(AtJsonGetObjectMember(Call->InputResponses, "consent", PH_JSON_OBJECT_TYPE_OBJECT));
}
// Expired, mismatched, or answered without a response: ask again rather than failing.
}
pending = AtpAllocatePendingConsent(connection);
PhQueryUnbiasedInterruptTime(&now);
pending->Used = TRUE;
pending->Action = Action->Action;
memcpy(pending->Identity, identity, sizeof(identity));
// A full 64 bits in one call.
pending->Nonce = PhGenerateRandomNumber64();
pending->Expiry.QuadPart = now.QuadPart + (LONGLONG)AT_PENDING_CONSENT_TIMEOUT_MS * PH_TIMEOUT_MS;
PhInitFormatI64X(&format[0], pending->Nonce);
requestState = PhFormat(format, RTL_NUMBER_OF(format), 20);
result = PhCreateJsonObject();
PhAddJsonObject(result, "resultType", "input_required");
inputRequests = PhCreateJsonObject();
request = PhCreateJsonObject();
PhAddJsonObject(request, "method", "elicitation/create");
PhAddJsonObjectValue(request, "params", AtpCreateElicitationParams(Call, Action, Target, TRUE));
PhAddJsonObjectValue(inputRequests, "consent", request);
PhAddJsonObjectValue(result, "inputRequests", inputRequests);
AtJsonAddString(result, "requestState", requestState);
PhDereferenceObject(requestState);
AtpSendResult(connection, Call->IdJson, result, TRUE);
return AtConsentInputRequired;
}
AT_CONSENT_RESULT AtMcpElicitConsent(
_In_ PAT_TOOL_CALL Call,
_In_ PCAT_ACTION_INFO Action,
_In_opt_ PAT_TARGET Target
)
{
if (!Call->ClientElicitation)
return AtConsentElicitationRequired;
if (Call->Modern)
return AtpElicitModern(Call, Action, Target);
else
return AtpElicitLegacy(Call, Action, Target);
}