|
| 1 | +/* |
| 2 | + * Copyright 2026-2026 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.modelcontextprotocol.modern.server; |
| 6 | + |
| 7 | +import io.modelcontextprotocol.util.Assert; |
| 8 | +import reactor.util.annotation.Nullable; |
| 9 | + |
| 10 | +/** |
| 11 | + * The decoded {@code Mcp-Name} header of a JSON-RPC request over HTTP, or its absence or |
| 12 | + * malformed state. |
| 13 | + * <p> |
| 14 | + * An HTTP transport decodes the header and adds it to the transport context under |
| 15 | + * {@link #TRANSPORT_CONTEXT_KEY} on every JSON-RPC request. Transports without headers |
| 16 | + * use {@link #NOT_MIRRORED} instead. The transport only decodes and forwards this value; |
| 17 | + * features whose methods the transport does not validate (for example, {@code tasks/get}, |
| 18 | + * {@code tasks/update}, and {@code tasks/cancel}) must compare it against their request |
| 19 | + * parameters and treat {@link #isMalformed()} as a mismatch. |
| 20 | + * <p> |
| 21 | + * Header names are matched ignoring case. A present header either has a decoded value or |
| 22 | + * is malformed, never both. |
| 23 | + * |
| 24 | + * @author Neeraj Bhatt |
| 25 | + * @see <a href="https://modelcontextprotocol.io/seps/2243-http-standardization">SEP-2243: |
| 26 | + * HTTP Standardization</a> |
| 27 | + * @see <a href= |
| 28 | + * "https://modelcontextprotocol.io/seps/2663-tasks-extension#streamable-http-routing-headers">SEP-2663: |
| 29 | + * Streamable HTTP Routing Headers</a> |
| 30 | + */ |
| 31 | +public final class McpNameHeader { |
| 32 | + |
| 33 | + /** |
| 34 | + * The transport context key under which a transport provides the {@code Mcp-Name} |
| 35 | + * header of a JSON-RPC request. |
| 36 | + */ |
| 37 | + public static final String TRANSPORT_CONTEXT_KEY = McpNameHeader.class.getName(); |
| 38 | + |
| 39 | + /** |
| 40 | + * Marks a transport without headers, such as stdio. Compared by identity with |
| 41 | + * {@link #ABSENT} to distinguish it from an HTTP request without the header. |
| 42 | + */ |
| 43 | + public static final McpNameHeader NOT_MIRRORED = new McpNameHeader(State.NOT_MIRRORED, null); |
| 44 | + |
| 45 | + /** |
| 46 | + * Marks an HTTP request without the {@code Mcp-Name} header. Compared by identity |
| 47 | + * with {@link #NOT_MIRRORED} to distinguish the two states. |
| 48 | + */ |
| 49 | + public static final McpNameHeader ABSENT = new McpNameHeader(State.ABSENT, null); |
| 50 | + |
| 51 | + /** |
| 52 | + * Marks a present header whose value was missing, could not be decoded, or was sent |
| 53 | + * more than once. |
| 54 | + */ |
| 55 | + public static final McpNameHeader MALFORMED = new McpNameHeader(State.MALFORMED, null); |
| 56 | + |
| 57 | + private final State state; |
| 58 | + |
| 59 | + @Nullable |
| 60 | + private final String value; |
| 61 | + |
| 62 | + private McpNameHeader(State state, @Nullable String value) { |
| 63 | + this.state = state; |
| 64 | + this.value = value; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Creates a header holding a decoded value. |
| 69 | + * @param decodedValue the decoded {@code Mcp-Name} value, not {@code null} |
| 70 | + * @return the present header |
| 71 | + */ |
| 72 | + public static McpNameHeader of(String decodedValue) { |
| 73 | + Assert.notNull(decodedValue, "decodedValue must not be null"); |
| 74 | + return new McpNameHeader(State.PRESENT, decodedValue); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Whether the header was sent, whether its value is valid or malformed. |
| 79 | + * @return {@code true} if the header is present |
| 80 | + */ |
| 81 | + public boolean isPresent() { |
| 82 | + return this.state == State.PRESENT || this.state == State.MALFORMED; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Whether the header was sent but had no valid decoded value. |
| 87 | + * @return {@code true} if the header is malformed |
| 88 | + */ |
| 89 | + public boolean isMalformed() { |
| 90 | + return this.state == State.MALFORMED; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * The decoded value of a present, valid header. |
| 95 | + * @return the decoded value, or {@code null} if the header is absent, malformed, or |
| 96 | + * the transport has no headers |
| 97 | + */ |
| 98 | + @Nullable |
| 99 | + public String value() { |
| 100 | + return this.value; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public String toString() { |
| 105 | + return "McpNameHeader[" + this.state + "]"; |
| 106 | + } |
| 107 | + |
| 108 | + private enum State { |
| 109 | + |
| 110 | + NOT_MIRRORED, ABSENT, MALFORMED, PRESENT |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments