Skip to content
This repository was archived by the owner on Apr 11, 2026. It is now read-only.
Merged
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
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,63 @@ public List<String> smartComplete(

This feature enables context-aware MCP operations where the behavior can be customized based on client-provided metadata such as user identity, preferences, session information, or any other contextual data.

#### metaProvider (_meta) Support

The `@McpTool`, `@McpPrompt`, and `@McpResource` annotations support a `metaProvider` attribute that allows attaching arbitrary `_meta` data to MCP tool, prompt, and resource declarations. This metadata is propagated to both the declarations and to resource content in `ReadResourceResult`.

To use this feature, implement the `MetaProvider` interface and reference the class in the annotation:

```java
class MyMetaProvider implements MetaProvider {
@Override
public Map<String, Object> getMeta() {
return Map.of(
"openai/widgetPrefersBorder", true,
"openai/widgetDomain", "https://chatgpt.com"
);
}
}
```

**Tool with _meta:**
```java
@McpTool(name = "my-tool",
description = "A tool with metadata",
metaProvider = MyMetaProvider.class)
public String myTool(@McpToolParam(description = "Input", required = true) String input) {
return "Processed: " + input;
}
```

**Prompt with _meta:**
```java
@McpPrompt(name = "my-prompt",
description = "A prompt with metadata",
metaProvider = MyMetaProvider.class)
public GetPromptResult myPrompt(
@McpArg(name = "topic", required = true) String topic) {
return new GetPromptResult("My Prompt",
List.of(new PromptMessage(Role.ASSISTANT, new TextContent("About: " + topic))));
}
```

**Resource with _meta:**
```java
@McpResource(uri = "data://{id}",
name = "My Resource",
description = "A resource with metadata",
metaProvider = MyMetaProvider.class)
public String getData(String id) {
return "Data for: " + id;
}
```

**MetaProvider behavior:**
- **Default**: `DefaultMetaProvider.class` — returns `null`, meaning no `_meta` is appended to declarations
- **Custom**: Implement `MetaProvider` and return a `Map<String, Object>` with the desired key-value pairs
- The provider must have a public no-arg constructor (it is instantiated via reflection)
- Metadata is propagated to both the MCP declaration and to resource content in `ReadResourceResult`

#### McpRequestContext Support

The library provides unified request context interfaces (`McpSyncRequestContext` and `McpAsyncRequestContext`) that offer a higher-level abstraction over the underlying MCP infrastructure. These context objects provide convenient access to:
Expand Down Expand Up @@ -2275,6 +2332,7 @@ Override `AbstractMcpToolProvider#doGetToolCallException()` to customize the exc
- **Sampling support** - Handle sampling requests from MCP servers
- **Progress notification support** - Handle progress notifications for long-running operations
- **Tool list changed support** - Handle tool list change notifications from MCP servers when tools are dynamically added, removed, or modified
- **_meta support via metaProvider** - Attach arbitrary `_meta` data to tool, prompt, and resource declarations using a pluggable `MetaProvider` interface on `@McpTool`, `@McpPrompt`, and `@McpResource`

## Requirements

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
* {@link IllegalArgumentException IllegalArgumentExceptions}. This class is stateless and
* not intended to be instantiated.
* </p>
*
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public final class MetaUtils {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* Utility class for adapting between McpPrompt annotations and McpSchema.Prompt objects.
*
* @author Christian Tzolov
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public class PromptAdapter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
package org.springaicommunity.mcp.adapter;

import java.util.List;
import java.util.Map;

import io.modelcontextprotocol.spec.McpSchema;
import io.modelcontextprotocol.util.Utils;
import org.springaicommunity.mcp.MetaUtils;
import org.springaicommunity.mcp.annotation.McpResource;
import org.springaicommunity.mcp.method.tool.utils.JsonParser;

/**
* Utility class that converts {@link McpResource} annotations into MCP schema objects.
* Provides factory methods to build {@link McpSchema.Resource} and
* {@link McpSchema.ResourceTemplate} instances from annotation metadata, including URI,
* name, description, MIME type, annotations, and optional {@code _meta} fields.
*
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public class ResourceAdapter {

Expand Down Expand Up @@ -66,12 +70,4 @@ public static McpSchema.ResourceTemplate asResourceTemplate(McpResource mcpResou
.build();
}

@SuppressWarnings("unchecked")
private static Map<String, Object> parseMeta(String metaJson) {
if (!Utils.hasText(metaJson)) {
return null;
}
return JsonParser.fromJson(metaJson, Map.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* Marks a method as a MCP Prompt.
*
* @author Christian Tzolov
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
*/
package org.springaicommunity.mcp.annotation;

import org.springaicommunity.mcp.context.DefaultMetaProvider;
import org.springaicommunity.mcp.context.MetaProvider;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map;

import org.springaicommunity.mcp.context.DefaultMetaProvider;
import org.springaicommunity.mcp.context.MetaProvider;

/**
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
* Use this when your tool, prompt, or resource does not need to expose any meta
* information or you want to keep responses minimal by default.
* </p>
*
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public class DefaultMetaProvider implements MetaProvider {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
/**
* Common interface for classes that provide metadata for the "_meta" field. This metadata
* is used in tool, prompt, and resource declarations.
*
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public interface MetaProvider {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
*
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public abstract class AbstractMcpResourceMethodCallback {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
*
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public final class AsyncMcpResourceMethodCallback extends AbstractMcpResourceMethodCallback
implements BiFunction<McpAsyncServerExchange, ReadResourceRequest, Mono<ReadResourceResult>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
*
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public final class AsyncStatelessMcpResourceMethodCallback extends AbstractMcpResourceMethodCallback
implements BiFunction<McpTransportContext, ReadResourceRequest, Mono<ReadResourceResult>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public class DefaultMcpReadResourceResultConverter implements McpReadResourceResultConverter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public interface McpReadResourceResultConverter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public final class SyncMcpResourceMethodCallback extends AbstractMcpResourceMethodCallback
implements BiFunction<McpSyncServerExchange, ReadResourceRequest, ReadResourceResult> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
*
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public final class SyncStatelessMcpResourceMethodCallback extends AbstractMcpResourceMethodCallback
implements BiFunction<McpTransportContext, ReadResourceRequest, ReadResourceResult> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
*
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public class AsyncMcpResourceProvider {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
*
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public class AsyncStatelessMcpResourceProvider {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
/**
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public class SyncMcpResourceProvider {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
*
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public class SyncStatelessMcpResourceProvider {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
/**
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public abstract class AbstractMcpToolProvider {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
/**
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public class AsyncMcpToolProvider extends AbstractMcpToolProvider {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
*
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public class AsyncStatelessMcpToolProvider extends AbstractMcpToolProvider {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
/**
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public class SyncMcpToolProvider extends AbstractMcpToolProvider {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
*
* @author Christian Tzolov
* @author Alexandros Pappas
* @author Vadzim Shurmialiou
* @author Craig Walls
*/
public class SyncStatelessMcpToolProvider extends AbstractMcpToolProvider {

Expand Down