|
| 1 | +--- |
| 2 | +hide: |
| 3 | + - navigation |
| 4 | + - toc |
| 5 | +--- |
| 6 | + |
| 7 | +# Core Components |
| 8 | + |
| 9 | +MCP (Model Context Protocol) defines three core component types, and this SDK simplifies the creation process of these components through annotations. |
| 10 | + |
| 11 | +## Resources |
| 12 | + |
| 13 | +Resource components are used to expose data to LLMs, similar to GET requests in Web APIs. |
| 14 | + |
| 15 | +### Basic Usage |
| 16 | + |
| 17 | +```java |
| 18 | +import com.github.thought2code.mcp.annotated.annotation.McpResource; |
| 19 | + |
| 20 | +public class MyResources { |
| 21 | + @McpResource(uri = "system://info", description = "System information") |
| 22 | + public Map<String, String> getSystemInfo() { |
| 23 | + Map<String, String> info = new HashMap<>(); |
| 24 | + info.put("os", System.getProperty("os.name")); |
| 25 | + info.put("java", System.getProperty("java.version")); |
| 26 | + info.put("cores", String.valueOf(Runtime.getRuntime().availableProcessors())); |
| 27 | + return info; |
| 28 | + } |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +### Annotation Explanation |
| 33 | + |
| 34 | +- `@McpResource`: Marks a method as an MCP resource |
| 35 | +- `uri`: Unique identifier of the resource, following URI format |
| 36 | +- `description`: Resource description for LLM to understand the resource's purpose |
| 37 | + |
| 38 | +## Tools |
| 39 | + |
| 40 | +Tool components are used to execute operations or calculations, similar to POST requests in Web APIs. |
| 41 | + |
| 42 | +### Basic Usage |
| 43 | + |
| 44 | +```java |
| 45 | +import com.github.thought2code.mcp.annotated.annotation.McpTool; |
| 46 | +import com.github.thought2code.mcp.annotated.annotation.McpToolParam; |
| 47 | + |
| 48 | +public class MyTools { |
| 49 | + @McpTool(description = "Calculate the sum of two numbers") |
| 50 | + public int add( |
| 51 | + @McpToolParam(name = "a", description = "First number", required = true) int a, |
| 52 | + @McpToolParam(name = "b", description = "Second number", required = true) int b |
| 53 | + ) { |
| 54 | + return a + b; |
| 55 | + } |
| 56 | + |
| 57 | + @McpTool(description = "Read complete file contents with UTF-8 encoding") |
| 58 | + public String readFile(@McpToolParam(name = "path", description = "File path", required = true) String path) { |
| 59 | + try { |
| 60 | + return Files.readString(Path.of(path)); |
| 61 | + } catch (IOException e) { |
| 62 | + return "Error reading file: " + e.getMessage(); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### Annotation Explanation |
| 69 | + |
| 70 | +- `@McpTool`: Marks a method as an MCP tool |
| 71 | +- `@McpToolParam`: Marks method parameters as tool parameters |
| 72 | + - `name`: Parameter name |
| 73 | + - `description`: Parameter description |
| 74 | + - `required`: Whether the parameter is required |
| 75 | + |
| 76 | +## Prompts |
| 77 | + |
| 78 | +Prompt components are used to define reusable prompt templates. |
| 79 | + |
| 80 | +### Basic Usage |
| 81 | + |
| 82 | +```java |
| 83 | +import com.github.thought2code.mcp.annotated.annotation.McpPrompt; |
| 84 | +import com.github.thought2code.mcp.annotated.annotation.McpPromptParam; |
| 85 | + |
| 86 | +public class MyPrompts { |
| 87 | + @McpPrompt(description = "Generate code for a given task") |
| 88 | + public String generateCode( |
| 89 | + @McpPromptParam(name = "language", description = "Programming language", required = true) String language, |
| 90 | + @McpPromptParam(name = "task", description = "Task description", required = true) String task |
| 91 | + ) { |
| 92 | + return String.format("Write %s code to: %s", language, task); |
| 93 | + } |
| 94 | + |
| 95 | + @McpPrompt(description = "Format text as specified style") |
| 96 | + public String formatText( |
| 97 | + @McpPromptParam(name = "text", description = "Text to format", required = true) String text, |
| 98 | + @McpPromptParam(name = "style", description = "Format style (e.g., formal, casual, technical)", required = true) String style |
| 99 | + ) { |
| 100 | + return String.format("Rewrite the following text in a %s style: %s", style, text); |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +### Annotation Explanation |
| 106 | + |
| 107 | +- `@McpPrompt`: Marks a method as an MCP prompt |
| 108 | +- `@McpPromptParam`: Marks method parameters as prompt parameters |
| 109 | + - `name`: Parameter name |
| 110 | + - `description`: Parameter description |
| 111 | + - `required`: Whether the parameter is required |
| 112 | + |
| 113 | +## Multilingual Support |
| 114 | + |
| 115 | +This SDK has built-in multilingual support, which can be enabled through the `@McpI18nEnabled` annotation. |
| 116 | + |
| 117 | +### Configure Multilingual |
| 118 | + |
| 119 | +```java |
| 120 | +@McpServerApplication |
| 121 | +@McpI18nEnabled(resourceBundleBaseName = "messages") |
| 122 | +public class I18nMcpServer { |
| 123 | + public static void main(String[] args) { |
| 124 | + McpServers.run(I18nMcpServer.class, args) |
| 125 | + .startStdioServer(McpServerInfo.builder() |
| 126 | + .name("i18n-server") |
| 127 | + .version("1.0.0") |
| 128 | + .build()); |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +### Internationalization Resource Files |
| 134 | + |
| 135 | +Create `messages.properties` file: |
| 136 | + |
| 137 | +```properties |
| 138 | +# messages.properties |
| 139 | +tool.add.description=Calculate the sum of two numbers |
| 140 | +tool.add.param.a.description=First number |
| 141 | +tool.add.param.b.description=Second number |
| 142 | +resource.system.info.description=System information |
| 143 | +prompt.generate.code.description=Generate code for a given task |
| 144 | +prompt.generate.code.param.language.description=Programming language |
| 145 | +prompt.generate.code.param.task.description=Task description |
| 146 | +``` |
| 147 | + |
| 148 | +Create `messages_zh_CN.properties` file: |
| 149 | + |
| 150 | +```properties |
| 151 | +# messages_zh_CN.properties |
| 152 | +tool.add.description=Calculate the sum of two numbers |
| 153 | +tool.add.param.a.description=First number |
| 154 | +tool.add.param.b.description=Second number |
| 155 | +resource.system.info.description=System information |
| 156 | +prompt.generate.code.description=Generate code for a given task |
| 157 | +prompt.generate.code.param.language.description=Programming language |
| 158 | +prompt.generate.code.param.task.description=Task description |
| 159 | +``` |
| 160 | + |
| 161 | +Using internationalized messages in components: |
| 162 | + |
| 163 | +```java |
| 164 | +@McpTool(description = "tool.add.description") |
| 165 | +public int add( |
| 166 | + @McpToolParam(name = "a", description = "tool.add.param.a.description") int a, |
| 167 | + @McpToolParam(name = "b", description = "tool.add.param.b.description") int b |
| 168 | +) { |
| 169 | + return a + b; |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +## Automatic Registration |
| 174 | + |
| 175 | +After defining MCP components, they will be automatically registered to the server. You just need to ensure that the component classes are in the package scanning path of the server application. |
| 176 | + |
| 177 | +If you need to specify a specific package path, you can use the following methods: |
| 178 | + |
| 179 | +```java |
| 180 | +@McpServerApplication(basePackageClass = MyMcpServer.class) |
| 181 | +// or |
| 182 | +@McpServerApplication(basePackage = "com.example.mcp.components") |
| 183 | +``` |
| 184 | + |
| 185 | +If no package path is specified, the package containing the main method will be scanned. |
0 commit comments