uid-generator-mcp 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- uid_generator_mcp-0.1.0/LICENSE +201 -0
- uid_generator_mcp-0.1.0/PKG-INFO +62 -0
- uid_generator_mcp-0.1.0/README.md +51 -0
- uid_generator_mcp-0.1.0/pyproject.toml +15 -0
- uid_generator_mcp-0.1.0/setup.cfg +4 -0
- uid_generator_mcp-0.1.0/tests/test_snowflake.py +92 -0
- uid_generator_mcp-0.1.0/uid_generator_mcp.egg-info/PKG-INFO +62 -0
- uid_generator_mcp-0.1.0/uid_generator_mcp.egg-info/SOURCES.txt +12 -0
- uid_generator_mcp-0.1.0/uid_generator_mcp.egg-info/dependency_links.txt +1 -0
- uid_generator_mcp-0.1.0/uid_generator_mcp.egg-info/requires.txt +1 -0
- uid_generator_mcp-0.1.0/uid_generator_mcp.egg-info/top_level.txt +1 -0
- uid_generator_mcp-0.1.0/uid_mcp/__init__.py +5 -0
- uid_generator_mcp-0.1.0/uid_mcp/server.py +68 -0
- uid_generator_mcp-0.1.0/uid_mcp/snowflake.py +144 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: uid-generator-mcp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Standalone snowflake unique-ID issuing MCP server (pure Python, no JVM).
|
|
5
|
+
License: Apache-2.0
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: mcp<2,>=1.0
|
|
10
|
+
Dynamic: license-file
|
|
11
|
+
|
|
12
|
+
<!-- mcp-name: io.github.tanbopiao/uid-generator-mcp -->
|
|
13
|
+
# uid-generator-mcp
|
|
14
|
+
|
|
15
|
+
Standalone MCP server that issues globally unique 64-bit IDs (snowflake
|
|
16
|
+
layout). Inspired by [baidu/uid-generator](https://github.com/baidu/uid-generator)
|
|
17
|
+
(Apache-2.0), preserved and re-implemented in pure Python — no JVM needed.
|
|
18
|
+
|
|
19
|
+
## Layout
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
sign(1) | timestamp(41, ms since 2026-01-01) | worker(10) | sequence(12)
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
- 4096 IDs per ms per worker, 1024 workers, ~69 years of headroom.
|
|
26
|
+
- Clock-rollback policy follows the upstream spirit: small rollbacks
|
|
27
|
+
(≤5 ms) spin-wait; larger rollbacks are refused with
|
|
28
|
+
`ClockRollbackError` instead of risking duplicates.
|
|
29
|
+
- Worker IDs auto-allocate against a local lease file so multiple
|
|
30
|
+
processes on one machine do not collide; override with
|
|
31
|
+
`UID_MCP_WORKER_ID`.
|
|
32
|
+
|
|
33
|
+
## Tools
|
|
34
|
+
|
|
35
|
+
| tool | purpose |
|
|
36
|
+
| --- | --- |
|
|
37
|
+
| `issue_id` | issue one unique ID |
|
|
38
|
+
| `issue_batch` | issue 1–1000 IDs, increasing in issue order |
|
|
39
|
+
| `decode` | parse an ID back to timestamp / worker / sequence |
|
|
40
|
+
| `worker_info` | show worker id and bit layout |
|
|
41
|
+
|
|
42
|
+
## Run
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install -e .
|
|
46
|
+
python run_server.py # stdio transport
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Tests
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
python3 -m unittest discover tests -v
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Covers: single-thread burst uniqueness (5000 IDs), 8-thread concurrency
|
|
56
|
+
uniqueness (4000 IDs), monotonicity, decode round-trip, worker range and
|
|
57
|
+
lease allocation.
|
|
58
|
+
|
|
59
|
+
## License
|
|
60
|
+
|
|
61
|
+
Apache-2.0. The upstream project is preserved under
|
|
62
|
+
[tanbopiao/uid-generator](https://github.com/tanbopiao/uid-generator).
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
<!-- mcp-name: io.github.tanbopiao/uid-generator-mcp -->
|
|
2
|
+
# uid-generator-mcp
|
|
3
|
+
|
|
4
|
+
Standalone MCP server that issues globally unique 64-bit IDs (snowflake
|
|
5
|
+
layout). Inspired by [baidu/uid-generator](https://github.com/baidu/uid-generator)
|
|
6
|
+
(Apache-2.0), preserved and re-implemented in pure Python — no JVM needed.
|
|
7
|
+
|
|
8
|
+
## Layout
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
sign(1) | timestamp(41, ms since 2026-01-01) | worker(10) | sequence(12)
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
- 4096 IDs per ms per worker, 1024 workers, ~69 years of headroom.
|
|
15
|
+
- Clock-rollback policy follows the upstream spirit: small rollbacks
|
|
16
|
+
(≤5 ms) spin-wait; larger rollbacks are refused with
|
|
17
|
+
`ClockRollbackError` instead of risking duplicates.
|
|
18
|
+
- Worker IDs auto-allocate against a local lease file so multiple
|
|
19
|
+
processes on one machine do not collide; override with
|
|
20
|
+
`UID_MCP_WORKER_ID`.
|
|
21
|
+
|
|
22
|
+
## Tools
|
|
23
|
+
|
|
24
|
+
| tool | purpose |
|
|
25
|
+
| --- | --- |
|
|
26
|
+
| `issue_id` | issue one unique ID |
|
|
27
|
+
| `issue_batch` | issue 1–1000 IDs, increasing in issue order |
|
|
28
|
+
| `decode` | parse an ID back to timestamp / worker / sequence |
|
|
29
|
+
| `worker_info` | show worker id and bit layout |
|
|
30
|
+
|
|
31
|
+
## Run
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install -e .
|
|
35
|
+
python run_server.py # stdio transport
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Tests
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
python3 -m unittest discover tests -v
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Covers: single-thread burst uniqueness (5000 IDs), 8-thread concurrency
|
|
45
|
+
uniqueness (4000 IDs), monotonicity, decode round-trip, worker range and
|
|
46
|
+
lease allocation.
|
|
47
|
+
|
|
48
|
+
## License
|
|
49
|
+
|
|
50
|
+
Apache-2.0. The upstream project is preserved under
|
|
51
|
+
[tanbopiao/uid-generator](https://github.com/tanbopiao/uid-generator).
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "uid-generator-mcp"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Standalone snowflake unique-ID issuing MCP server (pure Python, no JVM)."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "Apache-2.0" }
|
|
12
|
+
dependencies = ["mcp>=1.0,<2"]
|
|
13
|
+
|
|
14
|
+
[tool.setuptools.packages.find]
|
|
15
|
+
include = ["uid_mcp*"]
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"""Self-tests for the uid-generator-mcp engine. Run: python3 -m unittest discover tests -v"""
|
|
2
|
+
import os
|
|
3
|
+
import sys
|
|
4
|
+
import tempfile
|
|
5
|
+
import threading
|
|
6
|
+
import time
|
|
7
|
+
import unittest
|
|
8
|
+
|
|
9
|
+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
10
|
+
|
|
11
|
+
from uid_mcp.snowflake import ( # noqa: E402
|
|
12
|
+
EPOCH_MS,
|
|
13
|
+
MAX_WORKER,
|
|
14
|
+
ClockRollbackError,
|
|
15
|
+
IdGenerator,
|
|
16
|
+
allocate_worker_id,
|
|
17
|
+
decode_id,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class TestSnowflake(unittest.TestCase):
|
|
22
|
+
def test_uniqueness_under_burst(self):
|
|
23
|
+
gen = IdGenerator(worker_id=1)
|
|
24
|
+
ids = []
|
|
25
|
+
for _ in range(5):
|
|
26
|
+
ids.extend(gen.next_ids(1000))
|
|
27
|
+
self.assertEqual(len(ids), len(set(ids)), "duplicate ID in single-thread burst")
|
|
28
|
+
|
|
29
|
+
def test_multithread_uniqueness(self):
|
|
30
|
+
gen = IdGenerator(worker_id=2)
|
|
31
|
+
results = []
|
|
32
|
+
lock = threading.Lock()
|
|
33
|
+
|
|
34
|
+
def worker():
|
|
35
|
+
local = [gen.next_id() for _ in range(500)]
|
|
36
|
+
with lock:
|
|
37
|
+
results.extend(local)
|
|
38
|
+
|
|
39
|
+
threads = [threading.Thread(target=worker) for _ in range(8)]
|
|
40
|
+
for t in threads:
|
|
41
|
+
t.start()
|
|
42
|
+
for t in threads:
|
|
43
|
+
t.join()
|
|
44
|
+
self.assertEqual(len(results), 4000)
|
|
45
|
+
self.assertEqual(len(results), len(set(results)), "duplicate across threads")
|
|
46
|
+
|
|
47
|
+
def test_monotonic_in_issue_order(self):
|
|
48
|
+
gen = IdGenerator(worker_id=3)
|
|
49
|
+
ids = gen.next_ids(200)
|
|
50
|
+
self.assertEqual(ids, sorted(ids), "IDs not monotonic in issue order")
|
|
51
|
+
|
|
52
|
+
def test_decode_roundtrip(self):
|
|
53
|
+
gen = IdGenerator(worker_id=42)
|
|
54
|
+
before = int(time.time() * 1000)
|
|
55
|
+
value = gen.next_id()
|
|
56
|
+
after = int(time.time() * 1000)
|
|
57
|
+
info = decode_id(value)
|
|
58
|
+
self.assertEqual(info["worker_id"], 42)
|
|
59
|
+
self.assertGreaterEqual(info["timestamp_ms"], EPOCH_MS)
|
|
60
|
+
self.assertGreaterEqual(before, EPOCH_MS)
|
|
61
|
+
self.assertLessEqual(info["timestamp_ms"], max(after, before))
|
|
62
|
+
self.assertIn("iso_time", info)
|
|
63
|
+
|
|
64
|
+
def test_decode_rejects_negative(self):
|
|
65
|
+
with self.assertRaises(ValueError):
|
|
66
|
+
decode_id(-1)
|
|
67
|
+
|
|
68
|
+
def test_worker_id_range(self):
|
|
69
|
+
with self.assertRaises(ValueError):
|
|
70
|
+
IdGenerator(worker_id=MAX_WORKER + 1)
|
|
71
|
+
with self.assertRaises(ValueError):
|
|
72
|
+
IdGenerator(worker_id=-1)
|
|
73
|
+
|
|
74
|
+
def test_env_worker_override(self):
|
|
75
|
+
with tempfile.TemporaryDirectory() as d:
|
|
76
|
+
os.environ["UID_MCP_WORKER_ID"] = "777"
|
|
77
|
+
try:
|
|
78
|
+
gen = IdGenerator(state_dir=d)
|
|
79
|
+
self.assertEqual(gen.worker_id, 777)
|
|
80
|
+
finally:
|
|
81
|
+
del os.environ["UID_MCP_WORKER_ID"]
|
|
82
|
+
|
|
83
|
+
def test_allocate_worker_no_collision(self):
|
|
84
|
+
with tempfile.TemporaryDirectory() as d:
|
|
85
|
+
a = allocate_worker_id(d)
|
|
86
|
+
b = allocate_worker_id(d)
|
|
87
|
+
self.assertNotEqual(a, b, "two allocations collided in same state dir")
|
|
88
|
+
self.assertLessEqual(a, MAX_WORKER)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
if __name__ == "__main__":
|
|
92
|
+
unittest.main()
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: uid-generator-mcp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Standalone snowflake unique-ID issuing MCP server (pure Python, no JVM).
|
|
5
|
+
License: Apache-2.0
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: mcp<2,>=1.0
|
|
10
|
+
Dynamic: license-file
|
|
11
|
+
|
|
12
|
+
<!-- mcp-name: io.github.tanbopiao/uid-generator-mcp -->
|
|
13
|
+
# uid-generator-mcp
|
|
14
|
+
|
|
15
|
+
Standalone MCP server that issues globally unique 64-bit IDs (snowflake
|
|
16
|
+
layout). Inspired by [baidu/uid-generator](https://github.com/baidu/uid-generator)
|
|
17
|
+
(Apache-2.0), preserved and re-implemented in pure Python — no JVM needed.
|
|
18
|
+
|
|
19
|
+
## Layout
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
sign(1) | timestamp(41, ms since 2026-01-01) | worker(10) | sequence(12)
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
- 4096 IDs per ms per worker, 1024 workers, ~69 years of headroom.
|
|
26
|
+
- Clock-rollback policy follows the upstream spirit: small rollbacks
|
|
27
|
+
(≤5 ms) spin-wait; larger rollbacks are refused with
|
|
28
|
+
`ClockRollbackError` instead of risking duplicates.
|
|
29
|
+
- Worker IDs auto-allocate against a local lease file so multiple
|
|
30
|
+
processes on one machine do not collide; override with
|
|
31
|
+
`UID_MCP_WORKER_ID`.
|
|
32
|
+
|
|
33
|
+
## Tools
|
|
34
|
+
|
|
35
|
+
| tool | purpose |
|
|
36
|
+
| --- | --- |
|
|
37
|
+
| `issue_id` | issue one unique ID |
|
|
38
|
+
| `issue_batch` | issue 1–1000 IDs, increasing in issue order |
|
|
39
|
+
| `decode` | parse an ID back to timestamp / worker / sequence |
|
|
40
|
+
| `worker_info` | show worker id and bit layout |
|
|
41
|
+
|
|
42
|
+
## Run
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install -e .
|
|
46
|
+
python run_server.py # stdio transport
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Tests
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
python3 -m unittest discover tests -v
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Covers: single-thread burst uniqueness (5000 IDs), 8-thread concurrency
|
|
56
|
+
uniqueness (4000 IDs), monotonicity, decode round-trip, worker range and
|
|
57
|
+
lease allocation.
|
|
58
|
+
|
|
59
|
+
## License
|
|
60
|
+
|
|
61
|
+
Apache-2.0. The upstream project is preserved under
|
|
62
|
+
[tanbopiao/uid-generator](https://github.com/tanbopiao/uid-generator).
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
tests/test_snowflake.py
|
|
5
|
+
uid_generator_mcp.egg-info/PKG-INFO
|
|
6
|
+
uid_generator_mcp.egg-info/SOURCES.txt
|
|
7
|
+
uid_generator_mcp.egg-info/dependency_links.txt
|
|
8
|
+
uid_generator_mcp.egg-info/requires.txt
|
|
9
|
+
uid_generator_mcp.egg-info/top_level.txt
|
|
10
|
+
uid_mcp/__init__.py
|
|
11
|
+
uid_mcp/server.py
|
|
12
|
+
uid_mcp/snowflake.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
mcp<2,>=1.0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
uid_mcp
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""MCP server exposing unique-ID issuing tools (stdio transport).
|
|
2
|
+
|
|
3
|
+
Lineage: baidu/uid-generator (Apache-2.0) preserved by tanbopiao; this
|
|
4
|
+
server re-uses its snowflake ideas as a standalone Python service.
|
|
5
|
+
"""
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import os
|
|
9
|
+
|
|
10
|
+
from mcp.server.fastmcp import FastMCP
|
|
11
|
+
|
|
12
|
+
from .snowflake import ClockRollbackError, IdGenerator, MAX_WORKER, decode_id
|
|
13
|
+
|
|
14
|
+
mcp = FastMCP("uid-generator-mcp")
|
|
15
|
+
|
|
16
|
+
_state_dir = os.environ.get("UID_MCP_STATE_DIR") or os.path.dirname(
|
|
17
|
+
os.path.dirname(os.path.abspath(__file__))
|
|
18
|
+
)
|
|
19
|
+
_generator = IdGenerator(state_dir=_state_dir)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@mcp.tool()
|
|
23
|
+
def issue_id() -> dict:
|
|
24
|
+
"""Issue one globally unique 64-bit ID (snowflake layout)."""
|
|
25
|
+
try:
|
|
26
|
+
value = _generator.next_id()
|
|
27
|
+
except ClockRollbackError as e:
|
|
28
|
+
return {"error": str(e)}
|
|
29
|
+
info = decode_id(value)
|
|
30
|
+
return {"id": value, "worker_id": info["worker_id"], "iso_time": info["iso_time"]}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@mcp.tool()
|
|
34
|
+
def issue_batch(count: int = 10) -> dict:
|
|
35
|
+
"""Issue a batch of unique IDs (1-1000), strictly increasing in issue order."""
|
|
36
|
+
try:
|
|
37
|
+
values = _generator.next_ids(max(1, min(count, 1000)))
|
|
38
|
+
except ClockRollbackError as e:
|
|
39
|
+
return {"error": str(e)}
|
|
40
|
+
return {"ids": values, "count": len(values)}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@mcp.tool()
|
|
44
|
+
def decode(id_value: int) -> dict:
|
|
45
|
+
"""Decode a 64-bit ID into timestamp, worker id and sequence fields."""
|
|
46
|
+
try:
|
|
47
|
+
return decode_id(id_value)
|
|
48
|
+
except ValueError as e:
|
|
49
|
+
return {"error": str(e)}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@mcp.tool()
|
|
53
|
+
def worker_info() -> dict:
|
|
54
|
+
"""Show the current worker id and bit layout of this generator."""
|
|
55
|
+
return {
|
|
56
|
+
"worker_id": _generator.worker_id,
|
|
57
|
+
"max_worker_id": MAX_WORKER,
|
|
58
|
+
"layout": "sign(1) | timestamp(41, ms since 2026-01-01) | worker(10) | sequence(12)",
|
|
59
|
+
"throughput": "4096 IDs per ms per worker",
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def run() -> None:
|
|
64
|
+
mcp.run()
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
if __name__ == "__main__":
|
|
68
|
+
run()
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"""Snowflake-style unique ID engine.
|
|
2
|
+
|
|
3
|
+
Lineage: inspired by baidu/uid-generator (Apache-2.0), re-implemented in
|
|
4
|
+
pure Python for standalone MCP service. 64-bit layout:
|
|
5
|
+
|
|
6
|
+
sign(1) | timestamp(41, ms since epoch) | worker(10) | sequence(12)
|
|
7
|
+
|
|
8
|
+
Clock-rollback policy follows the upstream spirit: a small rollback
|
|
9
|
+
(< SPIN_MS) spin-waits for the clock to catch up; a larger rollback
|
|
10
|
+
raises ClockRollbackError instead of silently issuing duplicate-risk IDs.
|
|
11
|
+
"""
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import os
|
|
15
|
+
import threading
|
|
16
|
+
import time
|
|
17
|
+
|
|
18
|
+
EPOCH_MS = 1767225600000 # 2026-01-01T00:00:00Z, custom epoch
|
|
19
|
+
TIMESTAMP_BITS = 41
|
|
20
|
+
WORKER_BITS = 10
|
|
21
|
+
SEQUENCE_BITS = 12
|
|
22
|
+
|
|
23
|
+
MAX_WORKER = (1 << WORKER_BITS) - 1 # 1023
|
|
24
|
+
MAX_SEQUENCE = (1 << SEQUENCE_BITS) - 1 # 4095
|
|
25
|
+
MAX_TS_DELTA = (1 << TIMESTAMP_BITS) - 1 # ~69 years of ms
|
|
26
|
+
|
|
27
|
+
SPIN_MS = 5
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ClockRollbackError(RuntimeError):
|
|
31
|
+
"""System clock moved backwards beyond the tolerated spin window."""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class IdGenerator:
|
|
35
|
+
"""Thread-safe snowflake generator bound to one worker id."""
|
|
36
|
+
|
|
37
|
+
def __init__(self, worker_id: int | None = None, state_dir: str | None = None):
|
|
38
|
+
if worker_id is None:
|
|
39
|
+
worker_id = allocate_worker_id(state_dir)
|
|
40
|
+
if not 0 <= worker_id <= MAX_WORKER:
|
|
41
|
+
raise ValueError(f"worker_id must be in [0, {MAX_WORKER}], got {worker_id}")
|
|
42
|
+
self.worker_id = worker_id
|
|
43
|
+
self._lock = threading.Lock()
|
|
44
|
+
self._last_ms = -1
|
|
45
|
+
self._sequence = 0
|
|
46
|
+
|
|
47
|
+
def _current_ms(self) -> int:
|
|
48
|
+
return int(time.time() * 1000)
|
|
49
|
+
|
|
50
|
+
def next_id(self) -> int:
|
|
51
|
+
with self._lock:
|
|
52
|
+
ts = self._current_ms()
|
|
53
|
+
if ts < self._last_ms:
|
|
54
|
+
rollback = self._last_ms - ts
|
|
55
|
+
if rollback <= SPIN_MS:
|
|
56
|
+
# small rollback: spin until clock catches up
|
|
57
|
+
while ts < self._last_ms:
|
|
58
|
+
time.sleep(0.001)
|
|
59
|
+
ts = self._current_ms()
|
|
60
|
+
else:
|
|
61
|
+
raise ClockRollbackError(
|
|
62
|
+
f"clock rolled back {rollback}ms (> {SPIN_MS}ms); "
|
|
63
|
+
"refusing to issue IDs to avoid duplicates"
|
|
64
|
+
)
|
|
65
|
+
if ts == self._last_ms:
|
|
66
|
+
self._sequence += 1
|
|
67
|
+
if self._sequence > MAX_SEQUENCE:
|
|
68
|
+
# sequence exhausted in this ms: wait for next ms
|
|
69
|
+
while ts <= self._last_ms:
|
|
70
|
+
time.sleep(0.001)
|
|
71
|
+
ts = self._current_ms()
|
|
72
|
+
self._sequence = 0
|
|
73
|
+
else:
|
|
74
|
+
self._sequence = 0
|
|
75
|
+
self._last_ms = ts
|
|
76
|
+
delta = ts - EPOCH_MS
|
|
77
|
+
if delta < 0 or delta > MAX_TS_DELTA:
|
|
78
|
+
raise OverflowError(f"timestamp out of range: {ts}")
|
|
79
|
+
return (
|
|
80
|
+
(delta << (WORKER_BITS + SEQUENCE_BITS))
|
|
81
|
+
| (self.worker_id << SEQUENCE_BITS)
|
|
82
|
+
| self._sequence
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
def next_ids(self, count: int) -> list[int]:
|
|
86
|
+
if not 1 <= count <= 1000:
|
|
87
|
+
raise ValueError("count must be in [1, 1000]")
|
|
88
|
+
return [self.next_id() for _ in range(count)]
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def decode_id(id_value: int) -> dict:
|
|
92
|
+
"""Parse a 64-bit ID back into its fields."""
|
|
93
|
+
if not 0 <= id_value < (1 << 63):
|
|
94
|
+
raise ValueError("id must be a non-negative 63-bit integer")
|
|
95
|
+
sequence = id_value & MAX_SEQUENCE
|
|
96
|
+
worker = (id_value >> SEQUENCE_BITS) & MAX_WORKER
|
|
97
|
+
delta = id_value >> (WORKER_BITS + SEQUENCE_BITS)
|
|
98
|
+
ts_ms = EPOCH_MS + delta
|
|
99
|
+
return {
|
|
100
|
+
"id": id_value,
|
|
101
|
+
"timestamp_ms": ts_ms,
|
|
102
|
+
"worker_id": worker,
|
|
103
|
+
"sequence": sequence,
|
|
104
|
+
"iso_time": time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(ts_ms / 1000))
|
|
105
|
+
+ f".{ts_ms % 1000:03d}Z",
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
# ---------------------------------------------------------------------------
|
|
110
|
+
# worker id allocation: first-fit against a local state file so that several
|
|
111
|
+
# processes on the same machine do not collide by default.
|
|
112
|
+
# ---------------------------------------------------------------------------
|
|
113
|
+
STATE_FILE = ".uid_mcp_workers.json"
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def allocate_worker_id(state_dir: str | None = None) -> int:
|
|
117
|
+
env = os.environ.get("UID_MCP_WORKER_ID")
|
|
118
|
+
if env is not None:
|
|
119
|
+
wid = int(env)
|
|
120
|
+
if not 0 <= wid <= MAX_WORKER:
|
|
121
|
+
raise ValueError(f"UID_MCP_WORKER_ID out of range: {wid}")
|
|
122
|
+
return wid
|
|
123
|
+
import json
|
|
124
|
+
import uuid
|
|
125
|
+
|
|
126
|
+
directory = state_dir or os.getcwd()
|
|
127
|
+
path = os.path.join(directory, STATE_FILE)
|
|
128
|
+
me = uuid.uuid4().hex[:12]
|
|
129
|
+
now = time.time()
|
|
130
|
+
try:
|
|
131
|
+
with open(path) as f:
|
|
132
|
+
state = json.load(f)
|
|
133
|
+
except (OSError, ValueError):
|
|
134
|
+
state = {}
|
|
135
|
+
# drop leases older than 1 hour (crashed processes release automatically)
|
|
136
|
+
state = {k: v for k, v in state.items() if now - v.get("ts", 0) < 3600}
|
|
137
|
+
used = {v["worker"] for v in state.values()}
|
|
138
|
+
wid = next(i for i in range(MAX_WORKER + 1) if i not in used)
|
|
139
|
+
state[me] = {"worker": wid, "ts": now}
|
|
140
|
+
tmp = path + ".tmp"
|
|
141
|
+
with open(tmp, "w") as f:
|
|
142
|
+
json.dump(state, f)
|
|
143
|
+
os.replace(tmp, path)
|
|
144
|
+
return wid
|