@@ -622,6 +622,63 @@ function readStdinSync() {
622622 return Buffer . concat ( chunks ) . toString ( "utf8" ) ;
623623}
624624
625+ /**
626+ * Try to extract a specific field value from a JSON object string.
627+ *
628+ * When per-field stdin mode (`--key .`) is used and stdin contains a JSON
629+ * object, this function checks whether that object has the target key. If so,
630+ * it returns the field value, allowing agents to pipe a full JSON payload and
631+ * extract individual fields without the entire JSON string ending up as the
632+ * field value.
633+ *
634+ * Example: an agent may construct a full payload JSON and still pass individual
635+ * fields via `--key .`:
636+ * printf '{"title":"Fix bug","body":"Details here"}' \
637+ * | safeoutputs create_pull_request --title "Fix bug" --body .
638+ * Without this helper the body would be the raw JSON string. With it, the
639+ * body is correctly extracted as "Details here".
640+ *
641+ * Returns `undefined` when stdin is not a JSON object, when the key is absent
642+ * from the object, or when JSON parsing fails — in all those cases the caller
643+ * falls back to using the raw stdin content.
644+ *
645+ * @param {string } trimmedStdin - Pre-trimmed stdin content. Must have no
646+ * leading whitespace — the function uses a `startsWith('{')` fast-path to
647+ * avoid parsing non-JSON input, so leading whitespace will cause a JSON
648+ * object to be treated as non-JSON and return `undefined`. All callers
649+ * must pass `stdinContent.trim()` before invoking this function.
650+ * @param {string } canonicalKey - Canonical schema key to look up
651+ * @param {Record<string, {type?: string|string[]}> } schemaProperties - Tool input schema properties
652+ * @param {Map<string, string> } normalizedSchemaKeyMap - Map from normalized key to canonical schema key
653+ * @param {Set<string> } ambiguousNormalizedSchemaKeys - Normalized keys that map to multiple schema keys
654+ * @returns {unknown } The extracted field value, or `undefined` if not found
655+ */
656+ function tryExtractJsonFieldFromStdin ( trimmedStdin , canonicalKey , schemaProperties , normalizedSchemaKeyMap , ambiguousNormalizedSchemaKeys ) {
657+ if ( ! trimmedStdin . startsWith ( "{" ) ) {
658+ return undefined ;
659+ }
660+ try {
661+ const parsed = JSON . parse ( trimmedStdin ) ;
662+ if ( parsed === null || typeof parsed !== "object" || Array . isArray ( parsed ) ) {
663+ return undefined ;
664+ }
665+ // Try the canonical key first, then any schema-aliased form.
666+ if ( Object . prototype . hasOwnProperty . call ( parsed , canonicalKey ) ) {
667+ return parsed [ canonicalKey ] ;
668+ }
669+ // Search for a key in the JSON that resolves to the same canonical key.
670+ for ( const jsonKey of Object . keys ( parsed ) ) {
671+ const resolved = resolveSchemaPropertyKey ( jsonKey , schemaProperties , normalizedSchemaKeyMap , ambiguousNormalizedSchemaKeys ) ;
672+ if ( resolved === canonicalKey ) {
673+ return parsed [ jsonKey ] ;
674+ }
675+ }
676+ return undefined ;
677+ } catch {
678+ return undefined ;
679+ }
680+ }
681+
625682/**
626683 * Parse user-provided --key value pairs into a tool arguments object.
627684 * Supports both --key value and --key=value styles.
@@ -634,10 +691,15 @@ function readStdinSync() {
634691 * printf '{"issue_number":42,"body":"hello"}' | safeoutputs add_comment .
635692 *
636693 * When `stdinContent` is provided and non-empty, any '--key .' or '--key=.'
637- * pair substitutes that field's value with the raw stdin text (per-field
638- * stdin mode). This enables agents to pipe large text into a single field:
639- * printf 'Long issue body...' | safeoutputs create_issue --title "Bug" --body .
640- * When stdin is empty, the '.' is passed through as a literal value.
694+ * pair substitutes that field's value with stdin. If stdin is a JSON object
695+ * that contains the target key, the matching field value is extracted from the
696+ * JSON instead of using the entire stdin string. This prevents a common agent
697+ * mistake where the whole JSON payload ends up as a string field value:
698+ * printf '{"title":"Fix bug","body":"Details"}' \
699+ * | safeoutputs create_pull_request --title "Fix bug" --body .
700+ * When stdin is empty, the '.' is passed through as a literal value. When
701+ * stdin is non-empty but the key is absent from the JSON (or stdin is not a
702+ * JSON object), the entire trimmed stdin string is used as the field value.
641703 *
642704 * @param {string[] } args - User arguments after the tool name
643705 * @param {Record<string, {type?: string|string[]}> } [schemaProperties] - Tool input schema properties
@@ -686,7 +748,8 @@ function parseToolArgs(args, schemaProperties = {}, stdinContent = null) {
686748 const canonicalKey = resolveSchemaPropertyKey ( key , schemaProperties , normalizedSchemaKeyMap , ambiguousNormalizedSchemaKeys ) ;
687749 const rawValue = raw . slice ( eqIdx + 1 ) ;
688750 if ( rawValue === "." && trimmedStdin ) {
689- result [ canonicalKey ] = trimmedStdin ;
751+ const extracted = tryExtractJsonFieldFromStdin ( trimmedStdin , canonicalKey , schemaProperties , normalizedSchemaKeyMap , ambiguousNormalizedSchemaKeys ) ;
752+ result [ canonicalKey ] = extracted !== undefined ? extracted : trimmedStdin ;
690753 } else {
691754 result [ canonicalKey ] = coerceToolArgValue ( canonicalKey , rawValue , schemaProperties [ canonicalKey ] , result [ canonicalKey ] , ! hasSchemaProperties ) ;
692755 }
@@ -697,7 +760,8 @@ function parseToolArgs(args, schemaProperties = {}, stdinContent = null) {
697760 const canonicalKey = resolveSchemaPropertyKey ( raw , schemaProperties , normalizedSchemaKeyMap , ambiguousNormalizedSchemaKeys ) ;
698761 const rawValue = args [ i + 1 ] ;
699762 if ( rawValue === "." && trimmedStdin ) {
700- result [ canonicalKey ] = trimmedStdin ;
763+ const extracted = tryExtractJsonFieldFromStdin ( trimmedStdin , canonicalKey , schemaProperties , normalizedSchemaKeyMap , ambiguousNormalizedSchemaKeys ) ;
764+ result [ canonicalKey ] = extracted !== undefined ? extracted : trimmedStdin ;
701765 } else {
702766 result [ canonicalKey ] = coerceToolArgValue ( canonicalKey , rawValue , schemaProperties [ canonicalKey ] , result [ canonicalKey ] , ! hasSchemaProperties ) ;
703767 }
@@ -1441,6 +1505,7 @@ module.exports = {
14411505 parseToolArgs,
14421506 coerceToolArgValue,
14431507 unescapeCliStringArg,
1508+ tryExtractJsonFieldFromStdin,
14441509 extractJSONRPCMessages,
14451510 renderProgressMessages,
14461511 formatResponse,
0 commit comments