-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathllms-full.txt
More file actions
533 lines (423 loc) ยท 17.4 KB
/
llms-full.txt
File metadata and controls
533 lines (423 loc) ยท 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# mcp-annotated-java-sdk
Annotation-driven MCP (Model Context Protocol) SDK for Java that simplifies MCP server development.
## Overview
This SDK is a lightweight, annotation-based framework that simplifies MCP server development in Java. Define, develop and integrate your MCP Resources / Prompts / Tools with minimal code - No Spring Framework Required.
## What is MCP?
The Model Context Protocol (MCP) is a standardized protocol for building servers that expose data and functionality to LLM applications. Think of it like a web API, but specifically designed for LLM interactions.
## Key Advantages
- **No Spring Framework Required** - Pure Java, lightweight and fast
- **Instant MCP Server** - Get your server running with just 1 line of code
- **Zero Boilerplate** - No need to write low-level MCP SDK code
- **No JSON Schema** - Forget about complex and lengthy JSON definitions
- **Focus on Logic** - Concentrate on your core business logic
- **Spring AI Compatible** - Configuration file compatible with Spring AI Framework
- **Multilingual Support** - Built-in i18n support for MCP components
- **Type-Safe** - Leverage Java's type system for compile-time safety
## Comparison with Official MCP Java SDK
| Feature | Official MCP SDK | This SDK |
|---------|------------------|----------|
| Code Required | ~50-100 lines | ~5-10 lines |
| JSON Schema | Hand-coded JSON | No need to care |
| Type Safety | Limited | Full |
| Learning Curve | Steep | Gentle |
| Multilingual | Unsupported | Supported |
## Requirements
- **Java 17 or later** (required by official MCP Java SDK)
## Installation
### Maven
```xml
<dependency>
<groupId>io.github.thought2code</groupId>
<artifactId>mcp-annotated-java-sdk</artifactId>
<version>0.14.0</version>
</dependency>
```
### Gradle
```gradle
implementation 'io.github.thought2code:mcp-annotated-java-sdk:0.14.0'
```
## Quick Start Tutorial
### Step 1: Create Configuration File
Create `mcp-server.yml` in your `src/main/resources`:
```yaml
enabled: true
mode: STDIO
name: my-first-mcp-server
version: 1.0.0
type: SYNC
instructions: You are a helpful AI assistant
request-timeout: 20000
capabilities:
resource: true
prompt: true
tool: true
change-notification:
resource: true
prompt: true
tool: true
```
### Step 2: Create MCP Server Main Class
```java
@McpServerApplication
public class MyFirstMcpServer {
public static void main(String[] args) {
McpApplication.run(MyFirstMcpServer.class, args);
}
}
```
### Step 3: Define MCP Resources (Optional)
```java
public class MyResources {
@McpResource(uri = "system://info", description = "System information")
public Map<String, String> getSystemInfo() {
Map<String, String> info = new HashMap<>();
info.put("os", System.getProperty("os.name"));
info.put("java", System.getProperty("java.version"));
info.put("cores", String.valueOf(Runtime.getRuntime().availableProcessors()));
return info;
}
}
```
### Step 4: Define MCP Tools
```java
public class MyTools {
@McpTool(description = "Calculate the sum of two numbers")
public int add(
@McpToolParam(name = "a", description = "First number") int a,
@McpToolParam(name = "b", description = "Second number") int b
) {
return a + b;
}
@McpTool(description = "Read complete file contents with UTF-8 encoding")
public String readFile(
@McpToolParam(name = "path", description = "File path") String path
) {
try {
return Files.readString(Path.of(path));
} catch (IOException e) {
return "Error reading file: " + e.getMessage();
}
}
}
```
### Step 5: Define MCP Prompts (Optional)
```java
public class MyPrompts {
@McpPrompt(description = "Generate code for a given task")
public String generateCode(
@McpPromptParam(name = "language", description = "Programming language") String language,
@McpPromptParam(name = "task", description = "Task description") String task
) {
return String.format("Write %s code to: %s", language, task);
}
@McpPrompt(description = "Format text as specified style")
public String formatText(
@McpPromptParam(name = "text", description = "Text to format") String text,
@McpPromptParam(name = "style", description = "Format style (e.g., formal, casual, technical)") String style
) {
return String.format("Rewrite the following text in a %s style: %s", style, text);
}
}
```
### Step 6: Run the Server
```bash
# Compile your project
./mvnw clean package
```
Run `MyFirstMcpServer` from your IDE, or use `java -cp ...` with your compiled classes and dependencies on the classpath. Use an executable JAR setup in your own project if you need `java -jar` with a single file.
## Core Components
### Resources
Resource components are used to expose data to LLMs, similar to GET requests in Web APIs.
#### @McpResource Parameters
| Parameter | Description | Required |
|-----------|-------------|----------|
| `uri` | Unique identifier of the resource (URI format) | Yes |
| `description` | Resource description for LLM understanding | No (defaults to `name`, then method name) |
| `name` | Resource name (defaults to method name) | No |
| `title` | Resource title (defaults to `name`) | No |
| `mimeType` | MIME type of the resource content | No (default `text/plain`) |
### Tools
Tool components are used to execute operations or calculations, similar to POST requests in Web APIs.
#### @McpTool Parameters
| Parameter | Description | Required |
|-----------|-------------|----------|
| `description` | Tool description for LLM understanding | No (defaults to tool `name`, then method name) |
| `name` | Tool name (defaults to method name) | No |
| `title` | Tool title for display purposes | No |
#### @McpToolParam Parameters
| Parameter | Description | Required |
|-----------|-------------|----------|
| `name` | Parameter name | Yes |
| `description` | Parameter description | No (defaults to `name`) |
| `required` | Whether the parameter is required | No (default `true`) |
### Prompts
Prompt components are used to define reusable prompt templates.
#### @McpPrompt Parameters
| Parameter | Description | Required |
|-----------|-------------|----------|
| `description` | Prompt description for LLM understanding | No (defaults to prompt `name`, then method name) |
| `name` | Prompt name (defaults to method name) | No |
| `title` | Prompt title for display purposes | No |
#### @McpPromptParam Parameters
| Parameter | Description | Required |
|-----------|-------------|----------|
| `name` | Parameter name | Yes |
| `description` | Parameter description | No (defaults to `name`) |
| `required` | Whether the parameter is required | No (default `true`) |
### Completions
Completions provide auto-complete suggestions for resource URIs and prompt arguments.
Handlers must **return** `McpCompleteCompletion` and take **exactly one** parameter of type `McpSchema.CompleteRequest.CompleteArgument` (`name()` and `value()` identify the argument being completed).
#### Resource Completions
```java
import com.github.thought2code.mcp.annotated.annotation.McpResourceCompletion;
import com.github.thought2code.mcp.annotated.server.component.McpCompleteCompletion;
import io.modelcontextprotocol.spec.McpSchema;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
@McpResourceCompletion(uri = "file://")
public McpCompleteCompletion completeFileUri(McpSchema.CompleteRequest.CompleteArgument argument) {
String prefix = argument.value() != null ? argument.value() : "";
try {
List<String> paths =
Files.list(Paths.get(prefix.isEmpty() ? "." : prefix))
.map(Path::toString)
.limit(50)
.collect(Collectors.toList());
return McpCompleteCompletion.builder()
.values(paths)
.total(paths.size())
.hasMore(false)
.build();
} catch (Exception e) {
return McpCompleteCompletion.empty();
}
}
```
#### Prompt Completions
`@McpPromptCompletion.name` must match the **registered prompt name** (by default, the Java method name of the `@McpPrompt` method). Filter by `argument.name()` when one prompt has multiple parameters.
```java
import com.github.thought2code.mcp.annotated.annotation.McpPromptCompletion;
import com.github.thought2code.mcp.annotated.server.component.McpCompleteCompletion;
import io.modelcontextprotocol.spec.McpSchema;
import java.util.List;
@McpPromptCompletion(name = "generateCode")
public McpCompleteCompletion completeGenerateCode(McpSchema.CompleteRequest.CompleteArgument argument) {
if (!"language".equals(argument.name())) {
return McpCompleteCompletion.empty();
}
return McpCompleteCompletion.builder()
.values(List.of("Java", "Python", "JavaScript", "Go", "Rust"))
.total(5)
.hasMore(false)
.build();
}
```
## Server Modes
If `mode` is omitted in `mcp-server.yml`, the server defaults to **STREAMABLE**.
| Mode | Description | Use Case |
|------|-------------|----------|
| **STDIO** | Standard input/output communication | CLI tools, local development |
| **SSE** | Server-Sent Events (HTTP-based) | Real-time web applications (deprecated) |
| **STREAMABLE** | HTTP streaming | Web applications, recommended for production |
### STDIO Mode
```yaml
mode: STDIO
```
### SSE Mode (Deprecated)
```yaml
mode: SSE
sse:
port: 8080
endpoint: /sse
message-endpoint: /mcp/message
base-url: http://localhost:8080
```
### STREAMABLE Mode
```yaml
mode: STREAMABLE
streamable:
mcp-endpoint: /mcp/message
disallow-delete: false
keep-alive-interval: 20000
port: 8080
```
## Configuration Properties
| Property | Description | Default |
|----------|-------------|---------|
| `enabled` | Enable/disable MCP server | `true` |
| `mode` | Server mode: `STDIO`, `SSE`, `STREAMABLE` | `STREAMABLE` |
| `name` | Server name | `mcp-server` |
| `version` | Server version | `1.0.0` |
| `type` | Server type: `SYNC`, `ASYNC` | `SYNC` |
| `instructions` | Instructions for the LLM client | (empty) |
| `request-timeout` | Request timeout in milliseconds | `20000` |
| `capabilities.resource` | Enable resource support | `true` |
| `capabilities.subscribe-resource` | Enable resource subscription | `true` |
| `capabilities.prompt` | Enable prompt support | `true` |
| `capabilities.tool` | Enable tool support | `true` |
| `capabilities.completion` | Enable completion support | `true` |
| `change-notification.resource` | Notify clients on resource change | `true` |
| `change-notification.prompt` | Notify clients on prompt change | `true` |
| `change-notification.tool` | Notify clients on tool change | `true` |
| `sse.message-endpoint` | SSE POST message path | `/mcp/message` |
| `sse.endpoint` | SSE stream path | `/sse` |
| `sse.base-url` | Public base URL for the SSE server | *(empty)* |
| `sse.port` | HTTP port for SSE mode | `8080` |
| `streamable.mcp-endpoint` | Streamable HTTP MCP path | `/mcp/message` |
| `streamable.disallow-delete` | Reject HTTP DELETE on session | `false` |
| `streamable.keep-alive-interval` | Keep-alive interval (ms) | `20000` |
| `streamable.port` | HTTP port for STREAMABLE mode | `8080` |
## Profile-based Configuration
```yaml
# mcp-server.yml (base configuration)
enabled: true
mode: STREAMABLE
name: my-mcp-server
version: 1.0.0
profile: dev
```
```yaml
# mcp-server-dev.yml (profile-specific configuration)
streamable:
port: 8080
```
## Multilingual Support (i18n)
### Enable i18n
```java
@McpServerApplication
@McpI18nEnabled(resourceBundleBaseName = "messages")
public class I18nMcpServer {
public static void main(String[] args) {
McpApplication.run(I18nMcpServer.class, args);
}
}
```
### Create Resource Bundles
```properties
# messages.properties
tool.add.description=Calculate the sum of two numbers
tool.add.param.a.description=First number
tool.add.param.b.description=Second number
```
```properties
# messages_zh_CN.properties
tool.add.description=่ฎก็ฎไธคไธชๆฐๅญ็ๅ
tool.add.param.a.description=็ฌฌไธไธชๆฐๅญ
tool.add.param.b.description=็ฌฌไบไธชๆฐๅญ
```
### Use i18n in Components
```java
@McpTool(description = "tool.add.description")
public int add(
@McpToolParam(name = "a", description = "tool.add.param.a.description") int a,
@McpToolParam(name = "b", description = "tool.add.param.b.description") int b
) {
return a + b;
}
```
## Structured Content
Tools can return structured content by returning a type that **implements** `McpStructuredContent` (often a `record` with `@McpJsonSchemaProperty` on fields). There is no `McpStructuredContent.of(...)` helper.
```java
import com.github.thought2code.mcp.annotated.annotation.McpJsonSchemaDefinition;
import com.github.thought2code.mcp.annotated.annotation.McpJsonSchemaProperty;
import com.github.thought2code.mcp.annotated.annotation.McpTool;
import com.github.thought2code.mcp.annotated.annotation.McpToolParam;
import com.github.thought2code.mcp.annotated.server.McpStructuredContent;
@McpJsonSchemaDefinition
public record User(
@McpJsonSchemaProperty(description = "User id") String id,
@McpJsonSchemaProperty(description = "Display name") String name)
implements McpStructuredContent {
@Override
public String asTextContent() {
return "User " + id + ": " + name;
}
}
@McpTool(description = "Get user details")
public User getUser(@McpToolParam(name = "id", description = "User ID") String id) {
return new User(id, "Ada");
}
```
## Error Handling
If a tool method **throws any exception**, the server returns a `CallToolResult` with `isError` set to `true` and a generic method-invocation error message (the exception message is not forwarded to the client today).
For expected failures such as validation, return a normal value (for example a `String`) so the tool call remains a successful result with `isError` false:
```java
@McpTool(description = "Divide two numbers")
public String divide(
@McpToolParam(name = "a", description = "Dividend") double a,
@McpToolParam(name = "b", description = "Divisor") double b) {
if (b == 0) {
return "Cannot divide by zero.";
}
return Double.toString(a / b);
}
```
## Automatic Registration
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.
### Specify Package Path
```java
@McpServerApplication(basePackageClass = MyMcpServer.class)
// or
@McpServerApplication(basePackage = "com.example.mcp.components")
```
If no package path is specified, the package containing the main method will be scanned.
## Project Structure
```
your-mcp-project/
โโโ pom.xml
โโโ src/
โ โโโ main/
โ โ โโโ java/
โ โ โ โโโ com/
โ โ โ โโโ example/
โ โ โ โโโ MyMcpServer.java # Main entry point
โ โ โ โโโ components/
โ โ โ โ โโโ MyResources.java # MCP Resources
โ โ โ โ โโโ MyTools.java # MCP Tools
โ โ โ โ โโโ MyPrompts.java # MCP Prompts
โ โ โ โโโ service/
โ โ โ โโโ BusinessLogic.java # Business logic
โ โ โโโ resources/
โ โ โโโ mcp-server.yml # MCP configuration
โ โ โโโ messages.properties # Internationalization messages
โ โโโ test/
โ โโโ java/
โ โโโ com/
โ โโโ example/
โ โโโ McpServerTest.java # Unit tests
โโโ target/
โโโ *.jar # Build output (name depends on your project)
```
## Custom JSON Schema
For complex tool parameter or result types, annotate the type with `@McpJsonSchemaDefinition` and annotate fields (or record components) with `@McpJsonSchemaProperty` (JSON types are inferred from Java types).
```java
import com.github.thought2code.mcp.annotated.annotation.McpJsonSchemaDefinition;
import com.github.thought2code.mcp.annotated.annotation.McpJsonSchemaProperty;
import com.github.thought2code.mcp.annotated.annotation.McpTool;
import com.github.thought2code.mcp.annotated.annotation.McpToolParam;
@McpJsonSchemaDefinition
public record CreateUserRequest(
@McpJsonSchemaProperty(description = "User name") String name,
@McpJsonSchemaProperty(description = "User age", required = false) int age,
@McpJsonSchemaProperty(description = "Email address") String email) {}
@McpTool(description = "Create a user")
public String createUser(
@McpToolParam(name = "user", description = "User payload") CreateUserRequest user) {
return "Created: " + user.name();
}
```
## Important Notes
1. **Deprecated API**: The `McpServers` class is deprecated. Use `McpApplication.run()` instead.
2. **Deprecated Mode**: SSE mode is deprecated. Use STREAMABLE mode for new projects.
3. **Default Required**: The default `required` value for `@McpToolParam`, `@McpPromptParam`, and `@McpJsonSchemaProperty` is `true`.
## Links
- **GitHub Repository**: https://github.com/thought2code/mcp-annotated-java-sdk
- **Official Documentation**: https://thought2code.github.io/mcp-annotated-java-sdk-docs
- **Examples Repository**: https://github.com/thought2code/mcp-java-sdk-examples
- **MCP Official Site**: https://modelcontextprotocol.io
- **MCP Java SDK**: https://github.com/modelcontextprotocol/java-sdk
## License
MIT License